PHP Multipart Email Attachment Problem

Hi,

I’m having a bit of a nightmare getting a form to html email working properly.

There are up to 5 uploads allowed by the form, although at the moment I’m only trying to process one until I get that working.

Here’s the the necessary code excerpts - the first part of the message works fine.


$msg="<html><body><p>Some Text</p></body></html>";
//unique ID for the email boundaries

$uid=md5(uniqid(time()));
$subject="Form Submitted Online";
$headers="From: "."Joe Bloggs"." <"."joe@bloggs.co.uk".">\\r\
";
$headers.="Reply-To: ". "joe@bloggs.co.uk" . "\\r\
";
$headers.="MIME-Version: 1.0\\r\
";
$headers.="Content-Type: multipart/mixed; boundary=\\"$uid\\"";
$bound="--".$uid."\\r\
";
$bound_last="--".$uid."--\\r\
";

//build the HTML message section
$message.= "If you can see this MIME than your client doesn't accept MIME types!\\r\
".$bound;
$message .= "Content-Type: text/html; charset=\\"iso-8859-1\\"\\r\
"."Content-Transfer-Encoding: 7bit\\r\
\\r\
".$msg.$bound;

//Process the file uploads
//This is working and copying successfully into the uploads directory

$file=file_get_contents($path_of_uploaded_file);

//headers for the email attachment
$message.="Content-Type: ".'image/jpg'."; name=\\"".$name_of_uploaded_file."\\"\\r\
";
$message.="Content-Transfer-Encoding: base64\\r\
";
$message.="Content-Disposition: attachment; filename=".'"'.$name_of_uploaded_file.'"'."\\r\
".chunk_split(base64_encode($file));
$message.=$bound_last;

mail ($recipient, $subject, $message, $headers);


When I run the script the email comes through. The html bit is fine but is followed by the attachment contents in a huge chunk of ascii text underneath the html, rather than as a file attachment.

I hope I’m doing something simple wrong but have been trying loads of tweaks from different tutorials, none of which are helping.

In case it’s relevant I’m on Outlook 2003 but have also checked via webmail and getting the same results.

Many thanks in advance for any assistance.

I just try out with a script from this site:

http://www.emanueleferonato.com/2008/07/22/sending-email-with-multiple-attachments-with-php/

and it tested fine.

Hi,

Thanks for the response. After a very pain-staking search for line breaks, white spaces and commas, I managed to get things working.