PHPMailer addStringEmbeddedImage并多次添加相同的映像

PHPMailer addStringEmbeddedImage并多次添加相同的映像,php,image,base64,phpmailer,Php,Image,Base64,Phpmailer,因此,在PHPmailer中,如果使用addStringEmbeddedImage并在数据部分对图像进行base64_解码,则可以在邮件正文中添加一个64base编码的图像。例: $mail->addStringEmbeddedImage(base64_decode($str), "img_".$i, "img_".$i,"base64",$type); 我从base64格式的文本编辑器中获取图像,然后用相应的容器替换图像 <img src=\"cid:img_".$i."\"

因此,在PHPmailer中,如果使用addStringEmbeddedImage并在数据部分对图像进行base64_解码,则可以在邮件正文中添加一个64base编码的图像。例:

$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"
——边界
内容类型:image/png;name=“img_1”
内容传输编码:base64
内容ID:“,$start,$end-$start+1);
//获取数据后的类型:
$start=strpos($str,“数据:”);
$end=strpos($str,“;”,$start);
$type=substr($str、$start+5、$end-$start-5);
//这就是我得到base64字符串的地方
$start=strpos($str,“base64”);
$end=strpos($str,“\”,$start);
$str=substr($str,$start+7,$end-$start-7);
$mail->addStringEmbeddedImage(base64_解码($str),“img_u”..$i,“img_u”..$i,“base64”,$type);
} 
感谢您的帮助和阅读。

我明白为什么您会在一封邮件中多次使用同一图像,但为什么您会多次附加同一图像来实现这一点?cid值的主要意义在于,它们允许您从多个位置引用同一图像,因此如果您有一个徽标图像,则在同时出现在页眉和页脚中时,您可以附加一次并从两个位置引用它,从而减少邮件大小。我怀疑您的问题是,您试图解决此问题,但通过附加具有两个cid值的相同图像数据失败了,但PHPMailer注意到数据是相同的,并且没有进行第二次附加,留下您的second cid不指向任何内容。您可以通过对两个图像标记使用相同的cid值来修复它

这应该有您提到的问题:

 //$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);  
        } 
$img=base64\u解码($str);
$mail->addStringEmbeddedImage($img,“img_1”,“img_1”,“base64”,“$type);
$mail->addStringEmbeddedImage($img,“img_2”,“img_2”,“base64”,“$type);
这应该起作用:

$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\u解码($str);
$mail->addStringEmbeddedImage($img,“img_1”,“img_1”,“base64”,“$type);

代码计算内容的SHA256散列,并使用该散列检查它们是否相同。

只是一个假设(我需要更多代码才能更精确):变量可能是通过引用而不是通过值使用的,这会导致变量在使用后被销毁,从而使其为空以供后续使用。我理解你的意思,但如果是这样的话,那么它将不会继续显示其他图像,因为我在一个周期内完成了所有图像。我将编辑帖子以显示e代码,谢谢!这很有意义!我以为PHP编译器会直接附加图像,不管它是否相同。非常感谢!
$img = base64_decode($str);
$mail->addStringEmbeddedImage($img, "img_1", "img_1", "base64", $type);

<img src="cid:img_1">
<img src="cid:img_1">