Reputation: 109
How do I embed Base64 Image Data into email using php mail?
<?php
$aImg = $_POST['aImage'];
$to = "[email protected]";
$subject = "Sending png image to email";
$message = "<html><head></head><body>";
$message .= '<img src="'.$aImg.'" alt="This is an Image" /></body></html>';
$headers = "Content-type: text/html";
if (mail($to, $subject, $message, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
After I run the codes, it shows "message successfully sent" but the image is not displayed out. It shows a small red cross image instead. I have been debugging for hours but still not able to get my image out. Currently, I'm sending the email to localhost for testing.
Upvotes: 2
Views: 2280
Reputation: 3719
This is a little sloppy. I wrote it / mashed it together a long time ago from code I found online, and might have broken it sitting here pulling out private info without my glasses on. :-) Things I learned doing this way back then: chunk_split, concatenation (.), The use of a random separator.
<?
$to="x"; // For the file to be sent.
$from="xx"; // For the from line on the received email
$name="name.ext";
$type="application/x-gzip";
$subject="subj"
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// open the file for a binary read
$file = fopen(**xxxxxxxxxx filepath xxxxxxxxxxxx**,'rb');
// read the file content into a variable
// $data = chunk_split(base64_encode(fread($file,filesize($file))));
// now we encode it and split it into acceptable length lines
//ALREADY DONE, MOVE UP A FEW LINES
// message body
$message = "Here's that File I promised you";
// build headers
$headers = "From: ".$from." \r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// put message body in mime boundries
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// attachment with mime babble
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$backfile}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
chunk_split(base64_encode(fread($file,filesize(**xxxxxxxxxx filepath xxxxxxxxxxxx**)))) . "\n\n" .
"--{$mime_boundary}--\n";
// close the file
fclose($file);
// send mail
mail($to, $subject, $message, $headers)
?>
Upvotes: 1
Reputation: 48357
If you had tried 'view source' it would have given you your first clue as to what happenned here.
This is not trivial, and there's multiple ways to solve the problem. But you've got a long journey ahead of you.
Assuming you want to include the image directly in the email rather than as a HTTP URL or attachment, then you'll need to include it as data url - which will only work in very recent browsers / mail agents.
Alternatively you can simply add it as an attachment to any email - but creating mime messages is not trivial - use one of the off-the-shelf packages such as swiftmailer or phpmailer.
A 3rd approach is to create an encapsulated MIME http web archive file, but I'm not aware of any off-the-shelf package fro creating such files in PHP. Also, I think these files are only supported by MSOutlook, MSIE and Opera (and even then there are lots of issues with the MS implementation).
Upvotes: 0