i’m writing a little mail plugin in php for user information and i want to integrate multiple images in base64 encoding, the problem is, that only the first image is integrated. Any solution? All paths are correct, and html is integrated successfull. If I exchange the order of the images integrated in code, the image displayed in Mail also changes, so both of them are available, but they’re not displayed at the same time.
<?php
$ImageLocation ="images/logo.gif";
$ImageLocationRight ="images/right2.jpg";
$ImgName = "logo.gif";
$ImgNameRight = "right2.jpg";
$MailFrom="Tool";
$MailFromAdr="no_reply@xyz.com";
$MailTo ="xyzr@xyz.com";
$MailToSubject = "$subject";
$CID = md5(uniqid (rand(), 1));
$mime_boundary = "" . md5(uniqid(mt_rand(), 1));
$Header= "From:$MailFrom<$MailFromAdr>\n";
$Header.= "X-mailer: PHP/" . phpversion(). "\n";
$Header.= "MIME-Version: 1.0\n";
$Header.= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"; type=\"text/plain\"\n";
$MailBody = "--".$mime_boundary."\n";
$MailBody.= "Content-Type: Text/HTML; charset=iso-8859-1$EOL";
$MailBody.= "Content-Transfer-Encoding: quoted-printable\n\n";
$MailBody.= file_get_contents("../mail/mail.htm");
$MailBody.= "\n\n";
$MailBody.= "--".$mime_boundary."\n";
$MailBody= str_replace("images/logo.gif", "cid:$CID.$ImgName", $MailBody);
$MailBody= str_replace("images/right2.jpg", "cid:$CID.$ImgNameRight", $MailBody);
$fpr = fopen ($ImageLocationRight, "rb");
$strr = fread ($fpr, filesize ($ImageLocationRight));
$datar = chunk_split(base64_encode($strr));
$content.= "Content-Type: image/jpg\n";
$content.= "Content-ID: <$CID.$ImgNameRight>\n";
$content.= "Content-Transfer-Encoding: base64\n";
$content.= "Content-Disposition: inline; filename=\"$ImgNameRight\"\n\n";
fclose($fpr);
$content.= $datar."\n";
$MailBody.= $content;
$MailBody.= "--".$mime_boundary."--\n";
$fp = fopen ($ImageLocation, "rb");
$str = fread ($fp, filesize ($ImageLocation));
$data = chunk_split(base64_encode($str));
$content = "";
$content.= "Content-Type: image/gif\n";
$content.= "Content-ID: <$CID.$ImgName>\n";
$content.= "Content-Transfer-Encoding: base64\n";
$content.= "Content-Disposition: inline; filename=\"$ImgName\"\n\n";
fclose($fp);
$content.= $data."\n";
$MailBody.= $content;
$MailBody.= "--".$mime_boundary."--\n";
echo $MailBody;
mail($MailTo, $MailToSubject, $MailBody, $Header);
?>
In case you haven’t found a solution for this, I ran into a similar problem and finally got what it is causing this!
Just after you attach your first image, just eliminate the closing dashes for the MIME boundary:
Instead of having this code:
Try this instead:
This is only for all the images before the last one, you must keep the closing dashes in the very last image.