Take the 2-minute tour ×

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);
$content = fread($handle, $file_size);
$content = chunk_split(base64_encode($content));
$name = basename($file);
// use different content types here
$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:


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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The headers show your variable $header starts with the content --38fd5b8f9b812b5db3ec89cd01b6bd9c which isn't right... the headers shouldn't start with the boundary, just the body (so the first entry should be MIME-Version: 1.0). hard to say where this is happening, especially given you removed the code formatting fix I added but switching the line:



$header= "MIME-Version: 1.0\r\n";

Should stop any previous definition of $header from creeping in there.

share|improve this answer
OK, now that's embarrasing... but you are right :). I tried to add $headers = '';. and I tried to do unset($headers), and they both didn't work, so I didn't even consider removing that. Thanks so much, it actually worked - I removed the '.' from the .= and all is good now. I will never again underestimate the little . again... –  Yanipan Apr 30 '12 at 17:44

Your Answer


