PHP Multipart Email Attachment Problem


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

$subject="Form Submitted Online";
$headers="From: "."Joe Bloggs"." <"."".">\\r\
$headers.="Reply-To: ". "" . "\\r\
$headers.="MIME-Version: 1.0\\r\
$headers.="Content-Type: multipart/mixed; boundary=\\"$uid\\"";

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

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


//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\

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:

and it tested fine.


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