-2

I wonder if anyone can help me? I have done a lot of research on outputting multiple attachments using PHP mail but I just cant quite get my upload form working correctly.

Background:

I have a form which allows you to upload multiple images. The form itself is fine and i have had working with one one image no problem. The form sends a e-mail with an attachment. The problem has arisen when trying to send multiple attachments.

I can only ever get it to send one.

I have a piece of code below which deals with going through the array of files and adding them to headers of the e-mail. (see Below)

    $headers = "MIME-Version: 1.0\r\n";
    $headers = "X-Mailer: PHP/" . phpversion()."\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
    $headers .= "From: Sell My Rolex <info@sellmyrolex.today>\r\n";

    $headers .= "--".md5('boundary1')."\r\n";
    $headers .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."\r\n\r\n";

    $headers .= "--".md5('boundary2')."\r\n";
    $headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
    $headers .= $message."\r\n\r\n"; 


    $x="0";    

    foreach($files as $file){   

        $handle = fopen($destination.$file, "r");      
        $content = fread($handle, filesize($destination.$file));
        fclose($handle); 

        $attachment = chunk_split(base64_encode($content));           

        $headers .= "Content-Type:".$types[$x]."; ";
        $headers .= "name=\"".$file."\"\r\n";
        $headers .= "Content-Transfer-Encoding:base64\r\n";
        $headers .= "Content-Disposition:attachment; ";
        $headers .= "filename=\"".$file."\"\r\n";
        $headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
        $headers .= $attachment."\n\n";            

    }

This it is sent out

 $message = wordwrap($message, 70);
 $mailSent = mail($to, $subject, $message, $headers);

However all i get in the e-mail above the main e-mail message is

Content-Type:image/png; name="image1.png" Content-Transfer-Encoding:base64 Content-Disposition:attachment; filename="image1.png" X-Attachment-Id:6299

--f2b57013356e3f84c2c6b1d9b77f95d2--Content-Type:image/png; name="cleanoffice.png" Content-Transfer-Encoding:base64 Content-Disposition:attachment; filename="cleanoffice.png" X-Attachment-Id:5833

Can anyone point me in the right direction? I know people may want to suggest using a library like PHPmailer rather than the basic php mail but i want to use php mail to get this complete.

Any help is much appreciated.

Thanks

user1680567
  • 49
  • 1
  • 7

1 Answers1

0

You need to include multiple boundaries to mark different attachments. The following gives you a good example of how it would look like.

http://www.qcode.co.uk/post/70

Scalable
  • 1,550
  • 4
  • 16
  • 29