Drummin Posted January 4, 2013 Share Posted January 4, 2013 I'm using a foreach loop to attach an array of images to email. Emails are being sent with attachments however I am short one attachment every time. I'm thinking it has to be the $mime_boundary (dashes) and I've tried many combinations and the commented out IF statement to no avail. Can someone spot the problem? The attachment section is as follows. # Attachments would go here $x = 0; foreach($files as $thefile){ $file = fopen($thefile,"rb"); $data = fread($file,filesize($thefile)); fclose($file); $data = chunk_split(base64_encode($data)); $body .= "Content-Type: {\"image/png\"};\n" . " name=\"$thefile\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$thefile\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; $body .= "--$mime_boundary\n"; /* if($x == (count($files)-1)) { $body .= "--$mime_boundary--\n"; } else { $body .= "-–$mime_boundary\n"; } $x++; */ } # End email $body .= "--$mime_boundary--\n"; # <-- Notice trailing --, required to close email body for mime's Quote Link to comment https://forums.phpfreaks.com/topic/272671-mime_boundary-for-attachment-loop/ Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 You can check if it's the boundary easily by 1) removing the boundary you insert before the attachments (which isn't shown in the code you posted) and 2) attaching the boundary at the beginning of the loop (instead of the end). Also, the headers are wrong. The attachment sans-boundaries should look like "Content-Type: image/png\n" . "Content-Disposition: attachment; filename=\"$thefile\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; Quote Link to comment https://forums.phpfreaks.com/topic/272671-mime_boundary-for-attachment-loop/#findComment-1403116 Share on other sites More sharing options...
Drummin Posted January 4, 2013 Author Share Posted January 4, 2013 Thank you SO MUCH requinix. The script as it was worked when I did a stand alone test in a plain text email. I had then converted the email to send both plain and html versions and that's when I ran into trouble. The boundary at the bottom of the loop was the major problem and I also changed the sans-boundaries as you suggested and everything works great. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/272671-mime_boundary-for-attachment-loop/#findComment-1403139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.