【发布时间】:2017-10-24 20:44:50
【问题描述】:
因此,在 PHPmailer 中,如果您使用 addStringEmbeddedImage 并在数据部分中对图像进行 base64_decode,则可以将 64base 编码图像添加到邮件正文中。例如:
$mail->addStringEmbeddedImage(base64_decode($str), "img_".$i, "img_".$i,"base64",$type);
我从文本编辑器中获取 base64 格式的图像,然后用相应的容器替换图像
<img src=\"cid:img_".$i."\" ".$height. " " .$width. ">"
而且它工作得很好,但是由于某种原因,只要我多次添加图像,它是下一个图像还是第一个和最后一个图像都没有关系,邮件只会保存(?)第一张图片。是不是没有显示它,因为在原始邮件中您可以看到其中仅指定了 1 个容器。
--boundary
Content-Type: image/png; name="img_1"
Content-Transfer-Encoding: base64
Content-ID: <img_1>
Content-Disposition: inline; filename="img_1"
如您所见,每张图片都有不同的 id(这是它们在电子邮件中的顺序),唯一可以判断是同一张图片的方法是解码后的 base64 图像,所以我真的不知道为什么会这样只有当它的图像相同时才会发生。我可以添加 img1----img2----img1----img3 它会显示 img1 ---- img2---- |emptycontainer|---- img3
编辑:作为参考,这是我用来提取所有图像的时间
//$body of the mail, all the images i want to replace have that alt"" at the start
while(strstr($body, "<img alt")){
$i++;
$start= strpos($body, "<img alt");
$end= strpos($body,">",$start);
$str = substr($body,$start,$end-$start);
$height = "";
$width = "";
if (strstr($str, "height:")){
$ini = strpos($str, "height:")+7;
$fin = strpos($str,";",$ini);
$height = "height= \"".substr($str,$ini,$fin-$ini-2)."\"";
}
if (strstr($str, "width:")){
$ini = strpos($str, "width:")+6;
$fin = strpos($str,"\"",$ini);
$width = "width= \"".substr($str,$ini,$fin-$ini-2)."\"";
}
//here i replace the whole image with a container
$body= substr_replace($body, "<img src=\"cid:img_".$i."\" ".$height. " " .$width. ">", $start,$end-$start+1);
//get the type after data:
$start= strpos($str, "data:");
$end= strpos($str,";",$start);
$type= substr($str, $start+5, $end-$start-5);
//and this is where i get the base64 string
$start= strpos($str, "base64,");
$end= strpos($str,"\"",$start);
$str = substr($str, $start+7, $end-$start-7);
$mail->addStringEmbeddedImage(base64_decode($str), "img_".$i, "img_".$i,"base64",$type);
}
感谢您的帮助并感谢您的阅读
【问题讨论】:
-
只是一个假设(我需要更多代码才能更准确):可能是变量是通过引用而不是通过值使用导致变量在使用后被销毁并因此离开它留空以供后续使用。
-
我明白你在说什么,但如果是这样的话,它就不会继续显示其他图像,因为我在一个周期内完成了所有这些图像。我将编辑帖子以显示代码,谢谢!
标签: php image base64 phpmailer