Jump to content

$mime_boundary for attachment loop


Drummin

Recommended Posts

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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.