因此,在PHPmailer中,如果使用addStringEmbeddedImage,则可以向邮件正文中添加一个64基编码的图像,而在数据部分,您可以将图像添加到base64_decode中。例如:
$mail->addStringEmbeddedImage(base64_decode($str), "img_".$i, "img_".$i,"base64",$type); 我用base64表单从文本编辑器中获取图像,然后用相应的容器替换图像
<img src=\"cid:img_".$i."\" ".$height. " " .$width. ">"它运行良好,但出于某种原因,当我多次添加图像时,无论是下一个图像还是第一个和最后一个图像,邮件都会保存(?)第一张照片。并不是它没有显示它,因为在原始邮件中,您可以看到它中只指定了一个容器。
--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--img1-我可以添加img3 1-img2-img2-img1-img1
编辑:所以作为参考,这是我用来提取所有图像的时候
//$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);
} 我很感谢你的帮助,谢谢你的阅读
发布于 2017-10-24 21:17:48
我明白为什么你会在一条消息中使用相同的图像,但是为什么你要多次附加相同的图像来达到这个目的呢?cid值的许多要点是,它们允许您引用来自多个地方的相同图像,因此,如果您有一个同时出现在页眉和页脚中的徽标图像,您可以将它附加一次,然后从两个位置引用它,从而减小消息大小。我怀疑您的问题是,您正试图解决这一问题,并通过使用两个cid值附加相同的图像数据而失败,但是PHPMailer注意到数据是相同的,而不是执行第二个附件,留下第二个cid没有指向任何东西。您可以通过对两个图像标记使用相同的cid值来修复它。
这应该有你提到的问题:
$img = base64_decode($str);
$mail->addStringEmbeddedImage($img, "img_1", "img_1", "base64", $type);
$mail->addStringEmbeddedImage($img, "img_2", "img_2", "base64", $type);
<img src="cid:img_1">
<img src="cid:img_2">这应该是可行的:
$img = base64_decode($str);
$mail->addStringEmbeddedImage($img, "img_1", "img_1", "base64", $type);
<img src="cid:img_1">
<img src="cid:img_1">代码计算内容的SHA256哈希,并使用该哈希来检查它们是否相同。
https://stackoverflow.com/questions/46919696
复制相似问题