www.webdeveloper.com
Results 1 to 7 of 7

Thread: Multiple File Attachments for Email Only

  1. #1
    Join Date
    Feb 2005
    Posts
    97

    Multiple File Attachments for Email Only

    Hi. I have form with multile file upload files (type="file") Each one attaches one file. I would like to submit all attachments to one email. I have code that attaches the first file but I need to create a file array.( $_FILES['fileuploader']['name'], $_FILES['fileuploader2']['name'], $_FILES['fileuploader3']['name']...) Any help?

    Code:
    <?php 
    
    $email=$_REQUEST['Email']; 
    $firstName=$_REQUEST['FName']; 
    $lastName=$_REQUEST['LName']; 
    $tmp = $_FILES['fileuploader']['tmp_name'] ;
    $sep = md5(time());
    $filename = $_FILES['fileuploader']['name']; 
    
    $filedata = file_get_contents($tmp); //Get file contents
    $fdata = chunk_split(base64_encode($filedata)); //Encode data into text form
    
    //Determine mime type
    $ext = explode('.', $filename);
    $ext = $ext[1];
    
    if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") {
    $mime_type = "image/jpeg";
    }
    elseif($ext == "gif" || $ext == "GIF") {
    $mime_type = "image/gif";
    }
    elseif($ext == "pdf" || $ext == "PDF") {
    $mime_type = "application/x-pdf";
    }
    elseif($ext == "doc" || $ext == "DOC") {
    $mime_type = "application/msword";
    }
    elseif($ext == "txt" || $ext == "TXT") {
    $mime_type = "text/plain";
    }
    else {
    exit("Error: Wrong file type!");
    }
    
    
    $to = "me@domain.com"; 
    $subject = "New Test"; 
    
    $message = "". 
    "Email: $email" . "\n" . 
    "First Name: $FName" . "\n" . 
    "Last Name: $LName"; 
    
        
    $attachments[] = Array( 
       'data' => $filedata, 
       'name' => $filename, 
       'type' => $mime_type 
    ); 
    
    
        //Generate a boundary string 
    
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    
    
        //Add the headers for a file attachment 
    
    
    $headers = "MIME-Version: 1.0\n" . 
               "From: {$from}\n" . 
                 "Cc: me@domain.com\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" . 
              $text . "\n\n"; 
    
    
        //Add sttachments 
    
    foreach($attachments as $attachment){ 
       $data = chunk_split(base64_encode($attachment['data'])); 
       $name = $attachment['name']; 
       $type = $attachment['type']; 
    
       $message .= "--{$mime_boundary}\n" . 
                  "Content-Type: {$type};\n" . 
                  " name=\"{$name}\"\n" .               
                  "Content-Transfer-Encoding: base64\n\n" . 
                  $data . "\n\n" ; 
    } 
    
    $message .= "--{$mime_boundary}--\n"; 
    mail($to, $subject, $message, $headers); 
    
    ?>

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I have two solutions for you.

    The key aspect of multiple file attachment handling is working with multipart boundries, i.e. knowing how to format the email. You've already got the MIME stuff!

    This is one of those situations where someone already thought of it, so my first advice is to integrate a PHP class someone else wrote to handle this task for you -or- look at a simple script someone else wrote without all the bells and whistles of the class, and modify your code accordingly:

    Here is a link to the class - you just include a file, initialize a the class object, pass attachment filespec and mime type and it does the rest.

    Here is a script similar to yours with multi-boundry attachment and mime type support but without the advanced error checking of the class.

    -jim

  3. #3
    Join Date
    Feb 2005
    Posts
    97
    Thanks alot! I've been looking over these scripts and trying to implement some of the functions. Looks like neither is based on a form field where the filenames, paths, are not predetermined. I'm wondering if one file upload field with multiple files is a better option.

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Of course you just use the class instead of your current form processing script - it handles it all, the page is well documented as to usage.

    Otherwise, this is not a simple copy/paste thing - you'll need to read the example code in the smaller script I provided, see how it's done, and merge relevant code with yours. Line 4 will be replaced by your post data, and see lines 13-35 to process multiple attachments in the email. Customize the headers as you see fit, using your own MIME type integrated where applicable. Or start over using that script as your code and integrate yours into it. Whatever.

    In your form append [] on the end of the name attribute value to create an array, i.e. "name=filespec[]" so on the back end it becomes "$_POST['filespec'] array you can loop through.

    On a side note -- as to the front end I find many users really like the scripts that present one form field/browse button initially, then adds another they keep adding on more attachments, with a "Remove" checkbox added on the end.

    Simple example here

    -jim

  5. #5
    Join Date
    Feb 2005
    Posts
    97
    Perfect example!! Thanks once again.

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Cheers.

    -jim

  7. #7
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by SrWebDeveloper View Post
    On a side note -- as to the front end I find many users really like the scripts that present one form field/browse button initially, then adds another they keep adding on more attachments, with a "Remove" checkbox added on the end.
    swfupload is great for multiple uploads too.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles