繁体   English   中英

PHPMailer addStringEmbeddedImage并多次添加同一图像

[英]PHPMailer addStringEmbeddedImage and adding the same image more than once

因此,在PHPmailer中,如果使用addStringEmbeddedImage,则可以在邮件正文中添加64base编码的图像,而在数据部分中,可以对图像进行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 ---- 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);  
        } 

感谢您的帮助,并感谢您阅读

我可以理解为什么您会在一条消息中多次使用同一张图片,但是为什么要多次附加同一张图片才能实现同一目的? 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哈希值,并使用该哈希值检查它们是否相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM