alexcroox Posted June 5, 2009 Share Posted June 5, 2009 Hi, I have managed to get a script working that allows me to attach a cv document and send off with php mail, however the text message I would like to accompany it in the body of the email does not appear. Can you see why? function sendCsv($the_file,$thetime) { $MIME_type = "text/csv"; // File Type $filename = "***-test$thetime.csv"; // Filename that will be used for the file as the attachment $from = "[email protected]"; // Who the email is from $subject = "Test "; // The Subject of the email $the_message = "Test"; // Message that the email has in it $to = "alex@***.co.uk"; ini_set("sendmail_from",$from); ini_set("SMTP","10.***.18"); /* do error checking here to make sure file exists, is readable, ... */ $full_path = $_SERVER['DOCUMENT_ROOT']."/$the_file"; $size = filesize($full_path); $handle = fopen($full_path, "r"); $attachment = fread($handle, $size); $boundary = uniqid(); $uid = md5(uniqid(time())); /* send the message */ $header = "From: test <website@***.co.uk>\n"; $header .= "Cc: ***@gmail.com\n"; $header .= "Bcc: kevin@***.co.uk\n"; $header .= "Reply-To: kevin@***.co.uk\n"; $header .= "Content-Type: multipart/mixed; boundary=\"$uid\"\n\n"; $header .= "This is a multi-part message in MIME format.\n"; $header .= "--$boundary\n"; $header .= "Content-Type: text/plain; charset=iso-8859-1\n"; $header .= "Content-Transfer-Encoding: 7bit\n\n"; $header .= "$the_message\n\n"; $header .= "--$uid\n"; $header .= "Content-Type: $MIME_type; name=\"$filename\"\n"; $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"$filename\"\n\n"; $header .= "$attachment\n\n"; $header .= "--$uid--"; mail($to, $subject, "", $header); } (attaching the file works perfectly, just my message does not show) Link to comment https://forums.phpfreaks.com/topic/161035-help-with-email-attachements-with-text-not-showing/ Share on other sites More sharing options...
alexcroox Posted June 5, 2009 Author Share Posted June 5, 2009 OK i feel like an idiot, did a 5minute look on http://en.wikipedia.org/wiki/MIME and managed to see that I was using two different boundary values. solved code was $header .= "This is a multi-part message in MIME format.\n"; $header .= "--$uid\n"; (rather than --$boundary) Sorry for adding to the forums database for no good reason Link to comment https://forums.phpfreaks.com/topic/161035-help-with-email-attachements-with-text-not-showing/#findComment-849852 Share on other sites More sharing options...
alexcroox Posted June 5, 2009 Author Share Posted June 5, 2009 Just when I was feeling smug I checked on a windows machine with outlook and the csv opens with lots of random characters! It works with MAC Mail and OpenOffice, but maybe the headers are causing it to mess up in outlook? Link to comment https://forums.phpfreaks.com/topic/161035-help-with-email-attachements-with-text-not-showing/#findComment-849860 Share on other sites More sharing options...
alexcroox Posted June 5, 2009 Author Share Posted June 5, 2009 ok so this is the code im using. It attaches the file and text fine in both mac mail and outlook, the attachment opens fine in mac mail/open office, but is corrupt(strange chars) in outlook/excel. function sendCsv($the_file,$thetime) { $MIME_type = "text/csv"; $filename = "keycare$thetime.csv"; $from = "website@***.co.uk"; $subject = "Key Care New Subscriptions"; $the_message = "Please find attached new Keycare subsciptions from ***.\n\nThis message is produced by an automated script, please do not reply to this message. Any questions please contact Kevin via email: kevin@***.co.uk or on 023 9***\n\n"; $to = "***@***.co.uk"; ini_set("sendmail_from",$from); ini_set("SMTP","10***18"); $full_path = $_SERVER['DOCUMENT_ROOT']."/$the_file"; $size = filesize($full_path); $handle = fopen($full_path, "rb"); $attachment = fread($handle, filesize($full_path)); //$attachment = chunk_split(base64_encode($attachment)); fclose($handle); $boundary = uniqid(); $uid = md5(uniqid(time())); $header = "From: key care <website@***.co.uk>\n"; $header .= "Cc: alex@***.co.uk\n"; $header .= "Bcc: kevin@***.co.uk\n"; $header .= "Reply-To: kevin@***.co.uk\n"; $header .= "Content-Type: multipart/mixed; boundary=\"$uid\"\r\n"; $header .= "This is a multi-part message in MIME format.\n"; $header .= "--$uid\n"; $header .= "Content-Type: text/plain; charset=iso-8859-1\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n"; $header .= "$the_message\n\n"; $header .= "--$uid\n"; $header .= "Content-Type: $MIME_type; name=\"$filename\"\n"; $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"$filename\"\n\n"; $header .= "$attachment\n\n"; $header .= "--$uid--"; mail($to, $subject, "", $header); } Link to comment https://forums.phpfreaks.com/topic/161035-help-with-email-attachements-with-text-not-showing/#findComment-849974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.