Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
包含多个图像的PHP HTML邮件存在问题_Php_Email - Fatal编程技术网

包含多个图像的PHP HTML邮件存在问题

包含多个图像的PHP HTML邮件存在问题,php,email,Php,Email,我正在用php编写一个用于用户信息的小邮件插件,我想在base64编码中集成多个图像,问题是,只集成了第一个图像。有解决办法吗?所有路径都正确,html集成成功。如果我交换代码中集成的图像的顺序,邮件中显示的图像也会发生变化,因此它们都可用,但不会同时显示 <?php $ImageLocation ="images/logo.gif"; $ImageLocationRight ="images/right2.jpg"; $ImgName = "logo.gif"; $Im

我正在用php编写一个用于用户信息的小邮件插件,我想在base64编码中集成多个图像,问题是,只集成了第一个图像。有解决办法吗?所有路径都正确,html集成成功。如果我交换代码中集成的图像的顺序,邮件中显示的图像也会发生变化,因此它们都可用,但不会同时显示

<?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);
 ?>

您好,您没有将图像与$mailbody字符串关联。您的代码应该是这样的

$MailBody .= str_replace("images/logo.gif", "cid:$CID.$ImgName", $MailBody); $MailBody .= str_replace("images/right2.jpg", "cid:$CID.$ImgNameRight", $MailBody); $MailBody.=str_replace(“images/logo.gif”,“cid:$cid.$ImgName”,“$MailBody”); $MailBody.=str_replace(“images/right2.jpg”,“cid:$cid.$ImgNameRight”,“$MailBody”);
如果你还没有找到解决办法,我遇到了一个类似的问题,最终得到了它的原因

在附加第一个图像之后,只需删除MIME边界的结束划线:

不要使用此代码:

$MailBody.= "--".$mime_boundary."--\n";
请尝试以下方法:

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

这仅适用于最后一张图像之前的所有图像,您必须在最后一张图像中保留结束破折号。

只是一个建议,如果您发送了大量邮件,您可能需要使用现有的邮件库来完成所有繁重的工作。我经常使用swiftMail,我不认为我必须这样做,因为str_replace只会替换HTML中的占位符,这是正确的。