【问题标题】:PHPMailer addStringEmbeddedImage and adding the same image more than oncePHPMailer addStringEmbeddedImage 并多次添加相同的图像
【发布时间】: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


【解决方案1】:

我明白为什么您会在一条消息中多次使用同一张图片,但为什么要多次附加同一张图片来实现这一点? 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 哈希值并使用它来检查它们是否相同。

【讨论】:

  • 这很有意义!我认为 phpmailer 只会附加图像,无论它是否相同。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多