#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    15
    Rep Power
    0

    sending email + multiple attachments using php


    Hi

    I need to receive form data, including two files and send this in an email. I know how to receive the info and the files and put them into a database, and i know how to send the info in an email BUT i dont know how to send the actual files as attachments in the email. I could put links to the files in the email and upload tyhe files to a server for retrieval but i would much prefer to attach them.

    I have been searching on the web and it seems I have to set up a MIME email or something but this is slightly over my head at this time. Could someone please set out the steps for me to send the email? some sample code would be great but i would like to be able to understand what is happening if it is not too complex!!

    the code i use to send email (without attachments) at the moment follows: It's an example contact form

    --------

    $mailto = "$the_recipients_email_address";
    $mailfrom = "From: The.websites.online.enquiry.form";
    $subject = "The websites online enquiry form";
    $body = "$name has successfully submitted an enquiry form from the website, here are their details: \n

    Contact Details:

    Name: $name \n
    Job title: $job_title \n
    Company: $company \n
    Street Address: $street_address \n
    Street Address Line 2: $address_line_2 \n
    Town/City: $town_city \n
    etc etc

    mail ($mailto, $subject, $body, $mailfrom);

    ---------

    i was kinda hoping i could use this and attach the files to it somehow but i am doubting whether this is possible after reading about MIME.

    Any help would be much appreciated.

    Cheers

    Gareth
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    58
    Rep Power
    16
    Might help...

    Code:
    <?php
    // Read POST request params into global vars
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    
    // Obtain file upload vars
    $fileatt      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];
    
    $headers = "From: Me<me@me.com>";
    
      // Read the file to be attached ('rb' = read binary)
      $file = fopen($fileatt,'rb');
      $data = fread($file,filesize($fileatt));
      fclose($file);
    
      // 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/html; charset=\"iso-8859-1\"\n" .
                 "Content-Transfer-Encoding: 7bit\n\n" .
                 $message . "\n\n";
    
      // Base64 encode the file data
      $data = chunk_split(base64_encode($data));
    
      // Add file attachment to the message
      $message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  "Content-Disposition: attachment;\n" .
                  " filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                  $data . "\n\n" .
                  "--{$mime_boundary}--\n";
    	
    // Send the message
    $recipient = "me@me.com";
    
    $ok = @mail($recipient, $subject, $message, $headers);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    15
    Rep Power
    0
    right

    i kinda edited it slightly cos i really need it to add two attachments each time. all i added was two extra bits

    1:

    // Obtain file upload vars
    $fileatt2 = $_FILES['fileatt2']['tmp_name'];
    $fileatt2_type = $_FILES['fileatt2']['type'];
    $fileatt2_name = $_FILES['fileatt2']['name'];

    and

    2.

    // Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt2_type};\n" .
    " name=\"{$fileatt2_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt2_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";



    i figured that this might allow the sending of two files?

    when i sent my form data to the page,

    the actual processing page showed my variables merged in with a page of code...dunno why - i didnt ask it to echo anything!

    i actually received an email and it had an attachment - the second one that was added - but when i opened the attachment (a .txt file) it was just a jumbled mass of code. Also - the body of the email had no line breaks. it was just the variables i had selected - one after the other. here is the code i used for the body of the message:

    $message = "A CV and profile has been sent to you from $name via the website, here are their details: \n


    Contact Details:



    Name: $name \n
    DOB: $dob \n
    Street Address: $street_address \n
    Street Address Line 2: $address_line_2 \n
    Town/City: $town_city \n
    County/Region: $county_region \n
    Post Code: $post_code \n
    Country: $country \n
    Email: $email \n
    Telephone: $telephone \n
    Fax: $fax \n
    Referred by: $referred_by \n
    Contact by email: $contact_by_email \n
    Contact by tel: $contact_by_tel \n
    Contact by fax: $contact_by_fax \n
    Contact by post: $contact_by_post \n



    CV:



    Qualifications: $qualifications \n

    Key Experience: $key_experience \n

    Current Employer: $current_employer \n
    Current Employers start date: $current_employer_dates_from \n
    Current Employers end date: $current_employer_dates_to \n
    Current Employers responsibilities: $current_employer_responsibilities \n

    Previous Employer: $previous_employer1 \n
    Previous Employers start date: $previous_employer1_dates_from \n
    Previous Employers end date: $previous_employer1_dates_to \n
    Previous Employers responsibilities: $previous_employer1_responsibilities \n

    Previous Employer2: $previous_employer2 \n
    Previous Employers2 start date: $previous_employer2_dates_from \n
    Previous Employers2 end date: $previous_employer2_dates_to \n
    Previous Employers2 responsibilities: $previous_employer2_responsibilities \n

    Additional Employers Information: $additional_employers_information \n



    Profile:



    $profile ";


    i then thought maybe you had missed a '.' from a line of code:

    // Add a multipart boundary above the plain message
    $message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime

    after message i thought it should be $message .="etc etc

    i tried that and the processing page loaded fine with no jumbled code but the email body was blank and the one attachment that was sent was all jumbled code. so guess i didnt fix much.

    any ideas?
    thanks for your help with this code by the way
    i can see that it almost does what i need...if only we can get it working 100%
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    15
    Rep Power
    0
    i tested it as u posted it above and it works but i really need that second attachment to be sent-any ideas how i could modify the code to do this?

    also - any ideas why line breaks dont seem to work?
    (my other post shows the code i use to create the email message text)
  8. #5
  9. Learn logic, not PHP!
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    Hong Kong SAR
    Posts
    1,006
    Rep Power
    15
    also - any ideas why line breaks dont seem to work?
    Because SMTP takes \r\n instead of \n

    Cheers
    error_reporting(E_ALL) and echo($debug_message) may help to heal your headache.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    15
    Rep Power
    0
    thanks
    ill change that asap

    dont suppose you have any ideas about modifying the code to send two attachments do you?
  12. #7
  13. No Profile Picture
    Rehistradong manggagamit ...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Cavite, Philippines
    Posts
    116
    Rep Power
    14
    I think you could loop this part:

    // Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    Just change the $fileatt_type, $fileatt_name, and oh teh $data, should be changed too....

    // Read the file to be attached ('rb' = read binary)
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    add that to the loop so...

    while (....there are files to attach.....) {
    // Read the file to be attached ('rb' = read binary)
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    // Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    }

    Get the general idea?

    The one I created sends files that are stored in the server after they are uploaded. Once they were sent, delete the files....

    Hope this helps...
    JP

IMN logo majestic logo threadwatch logo seochat tools logo