I'm following from an example of sending an email with image attachment through mail(). The email gets sent fine and the image is attached but when I attempt to open the image the browser tells me it's corrupt. I saved the image and opened it up in a text editor and the contents are still in base64, as seen in this snippet of the file: http://pastebin.com/B2VgarH8
The Content-Transfer-Encoding: base64 line I assumes tells the browser to interpret the image but it does nothing. I've tried opening it in Firefox and Chrome and it's the same result. Anyone have an idea why it's failing?
$to = 'admin@hostoi.com';
$subject = $matches[3][$i];
$bound_text = "AbC123";
$bound = "--".$bound_text."
";
$bound_last = "--".$bound_text."--
";
$headers = "From: me@gmail.com
";
$headers .= "MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$message = "If you can see this MIME than your client doesn't accept MIME types!
" . $bound;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"
" .
"Content-Transfer-Encoding: 7bit
" . (string)$matches[5][$i] . "
" . $bound;
$attachment = chunk_split(base64_encode(file_get_contents($matches[1][$i])));
$attachment_ext = substr(strrchr($matches[1][$i], '.'), 1);
$attachment_ext = $attachment_ext == 'jpg' ? 'jpeg' : $attachment_ext;
$attachment_name = time() . "_" . rand(10,99) . "." . $attachment_ext;
$message .= "Content-Type: image/$attachment_ext; name=\"$attachment_name\"
" .
"Content-Transfer-Encoding: base64
" .
"Content-disposition: attachment
" .
chunk_split(base64_encode($attachment)) . $bound_last;
if(mail($to, $subject, $message, $headers)) {
echo 'MAIL SENT';
//mysql_query("INSERT INTO message(body) VALUES(" . mysql_real_escape_string($matches[5][$i]) . ")", $dbh);
} else {
echo 'MAIL FAILED';
}