I’m having trouble with my emails with attached file.
The headers, including html(parsed as text) and file (also parsed as text), which are all in the headers, appear as the message body. this code used to work, but I can’t figure what changed.
There is a random factor, some times the mail do arrive correctly, but I can’t seem to figure out the pattern (assuming there is) that will ruin the mail/make it work.
this is my code to generate the mail:
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/html; charset=utf-8\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
// use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
$mailto = 'yanivshimony@gmail.com';
if (mail($mailto, $subject, "", $header)) {
echo "1"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
I tried removing all the headers, and was left only with the following headers making it work:
$header .= "Content-Type: text/html; charset=utf-8\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
so its quite usless, as its just a normal html email, with no multipart/attachment/anything – but the HTML is parsed 🙂
Any idea what could break the headers??
Thanks!
The headers show your variable
$headerstarts with the content--38fd5b8f9b812b5db3ec89cd01b6bd9cwhich isn’t right… the headers shouldn’t start with the boundary, just the body (so the first entry should beMIME-Version: 1.0). hard to say where this is happening, especially given you removed the code formatting fix I added but switching the line:To:
Should stop any previous definition of
$headerfrom creeping in there.