PHP HTML邮件问题,包括多个图像

PHP HTML邮件问题,包括多个图像

问题描述:

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>
";
  $Header.= "X-mailer: PHP/" . phpversion(). "
";  
  $Header.= "MIME-Version: 1.0
";
  $Header.= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"; type=\"text/plain\"
"; 

  $MailBody = "--".$mime_boundary."
";
  $MailBody.= "Content-Type: Text/HTML; charset=iso-8859-1$EOL";  
  $MailBody.= "Content-Transfer-Encoding: quoted-printable

";  
  $MailBody.= file_get_contents("../mail/mail.htm");
  $MailBody.= "

";
  $MailBody.= "--".$mime_boundary."
";  

  $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
";
  $content.= "Content-ID: <$CID.$ImgNameRight>
";
  $content.= "Content-Transfer-Encoding: base64
";
  $content.= "Content-Disposition: inline; filename=\"$ImgNameRight\"

";
  fclose($fpr);

  $content.= $datar."
";

  $MailBody.= $content;
  $MailBody.= "--".$mime_boundary."--
";

  $fp = fopen ($ImageLocation, "rb");
  $str = fread ($fp, filesize ($ImageLocation));
  $data = chunk_split(base64_encode($str));
  $content = "";
  $content.= "Content-Type: image/gif
";
  $content.= "Content-ID: <$CID.$ImgName>
";
  $content.= "Content-Transfer-Encoding: base64
";
  $content.= "Content-Disposition: inline; filename=\"$ImgName\"

";  
  fclose($fp);


  $content.= $data."
";

  $MailBody.= $content;
  $MailBody.= "--".$mime_boundary."--
";

  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:

$MailBody.= "--".$mime_boundary."--
";

Try this instead:

$MailBody.= "--".$mime_boundary."
";

This is only for all the images before the last one, you must keep the closing dashes in the very last image.

hi you are not concating the images with you $mailbody string. your code should be like this

$MailBody .= str_replace("images/logo.gif", "cid:$CID.$ImgName", $MailBody);
 $MailBody .= str_replace("images/right2.jpg", "cid:$CID.$ImgNameRight", $MailBody);