Link to home
Start Free TrialLog in
Avatar of Sideas
Sideas

asked on

Simple email script

I need to try and get a simple script to email a form and attach a file to it, if they choose to attach a file. Basically its a simple vacancy form where you can attach a CV. I have no experience in coding fom and stuff, so any help would be good. I usually pay to get this done, but the  budget does not allow for this, so i might have to try do it myself :0/

Thanks for any help.
ASKER CERTIFIED SOLUTION
Avatar of lucki_luke
lucki_luke
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you find it difficult to use any library, you can use this simple code


      <div style="height:15px"></div>

      <table width="80%" align="center" border="1" class="Tb1">
            <tr>
                  <td>
                        <form action="email.php?submit=yes" method="post" enctype="multipart/form-data" name="compose">
                        <table width="93%" align="center" border="1" class="Tb1">
                              <tr class="headerrow">
                                    <td colspan="2">
                                          Send Email                                    </td>                  
                              </tr>
                              <tr class="oddrow">
                                    <td>
                                          To:                                    </td>
                                    <td>
                                          <input type="text" tabindex="1" name="receiver" size="50">                                    </td>
                              </tr>
                              <tr class="evenrow">
                                    <td>
                                          Subject:                                    </td>
                                    <td>
                                          <input type="text" name="subject" size="50">                                    </td>
                              </tr>
                              <tr class="oddrow">
                                    <td>
                                          Message:                                    </td>
                                    <td>
                                          <textarea name="message" cols="45" rows="10"></textarea>                                    </td>
                              </tr>
                              <tr class="oddrow">
                                <td>Attatchment: </td>
                                <td><input id="fileatt" type="file" name="fileatt"  tabindex="" /></td>
                          </tr>
                              <tr class="evenrow">
                                    <td align="center" colspan="2">
                                          <input type="submit" name="send" value="Send" class="buttons">                                    </td>                                    
                              </tr>
                        </table>                        
                        </form>                  </td>
                  <form name="combo_box" action="send_email.php" method="post">
                  </form>
            </tr>
</table>      
<?
      if($_GET['submit'] == 'yes')
      {
            // Message Receiver
            $reciever = $_POST['receiver'];
            
            // Message Sender
            $subject = $_POST['subject'];
            
            // Message
            $message = $_POST['message'];
            
            // Obtain file upload vars
            $fileatt            = $_FILES['fileatt']['tmp_name'];
            $fileatt_type      = $_FILES['fileatt']['type'];
            $fileatt_name      = $_FILES['fileatt']['name'];            // Converting String of Receiver Name into Array

            $recievernames = explode(",", $reciever);

            // Iterating Receiver Name's Array
            $i = 0;
            while($recievernames[$i])
            {
                  $email = $recievernames[$i];
                  
                  $headers = "From: aamir@post.com";

                   if (is_uploaded_file($fileatt)) {
                        $file = fopen($fileatt,'rb');
                        $data = fread($file,filesize($fileatt));
                        fclose($file);
                  
                        // Base64 encode the file data
                        $data = chunk_split(base64_encode($data));
                    }                  

                    // Generate a boundary string
                    $semi_rand = md5(time());
                    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                    
                    // Add the headers for a file attachment
                    $headers .= "\nMIME-Version: 1.0\n" .
                                      "Content-Type: multipart/mixed;\n" .
                                      " boundary=\"{$mime_boundary}\"";
                  
                    // Add a multipart boundary above the plain message
                    $message = "This is a multi-part message in MIME format.\n\n" .
                                     "--{$mime_boundary}\n" .
                                     "Content-Type: text/plain;\n charset=\"iso-8859-1\"\n" .
                                     "Content-Transfer-Encoding: 7bit\n\n" .
                                     $message . "\n\n" .
                                     "--{$mime_boundary}\n";

                    // Add file attachment(s) to the message
                    if ($fileatt_name > "") {
                        $message .= "Content-Type: {$fileatt_type};\n" .
                                           " name=\"{$fileatt_name}\"\n" .
                                            "Content-Transfer-Encoding: base64\n" .
                                           "Content-Disposition: attachment;\n" .
                                           " filename=\"{$fileatt_name}\"\n\n" .
                                            $data . "\n" .
                                            "--{$mime_boundary}\n";
                        }
                  
                  // Mail it
                  mail($email, $subject, $message, $headers);                  

                  $i++;
            }            
                        
      }      
?>            
Avatar of Sideas
Sideas

ASKER

Hi guys,

Got so far on both of these but didn't get them working :-/

lucki_luke i got to here
http://www.idealogyltd.com/Email-form/email-form.html
I did put the sourceforge script up you can see it is there. http://www.idealogyltd.com/Email-form/

i_m_aamir i got to this point
http://www.idealogyltd.com/Email-form/email-form-2.html

Any help on both be great. I not sure what might be going wrong so don't know where to look. I guess its simple but as i don't know this stuff i cant figure it out.

Thanks again for trying to help a noob.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sideas

ASKER

I think i have amended them both correctly, now i get an error like so.

Warning: main(class.phpmailer.php): failed to open stream: No such file or directory in /data/webroot/default/Email-form/email.php on line 4
Fatal error: main(): Failed opening required 'class.phpmailer.php' (include_path='.:') in /data/webroot/default/Email-form/email.php on line 4

Think this is down to my URL or something. I guess this is quite hard.

Thanks for the offer ucitianz i cannot get you FTP access, company would not let it happen. Hopefully you guys can figure it out from there!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sideas

ASKER

Hi,

Changed this, error on line 35 now

Parse error: parse error, unexpected $ in /data/webroot/default/Email-form/email.php on line 35

Sorry about this!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sideas

ASKER

Hi,

That seems to have fixed it :0) not sure why it wasn't working, code looked the same.