php 如何使用 Phpmailer 附加图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6095285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:20:38  来源:igfitidea点击:

How can i attach an image using Phpmailer?

phpphpmailer

提问by Mary

I am using phphmailer and have attached an image, it shows only the image like an icon rather than image itself here is my code could you help.

我正在使用 phphmailer 并附加了一个图像,它只显示像图标一样的图像,而不是图像本身,这是我的代码,你能帮忙吗?

$mail->AddEmbeddedImage('2.jpg', '2img', '2.jpg');
$mail->Subject  =  "Order Form:  Contact form submitted";
$mail->Body     =  $body . 'img src="../../photo/2img" ;

note: i have dropped html tag befor img as I get error sending this Q.

注意:我在 img 之前删除了 html 标签,因为我在发送这个 Q 时遇到错误。

回答by Gabriel Sosa

Per PHPMailer Manual, you sould use the method AddEmbeddedImage

根据PHPMailer Manual,您应该使用该方法AddEmbeddedImage

$mail->AddEmbeddedImage(filename, cid, name);
By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg '); 

like this:

像这样:

$mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png");
$mail->Body = 'Embedded Image: <img alt="PHPMailer" src="cid:my-attach"> Here is an image!';

so cid:my-attachwill be replaced with the inline-source of the image that's inside the email body

socid:my-attach将被替换为电子邮件正文中的图像的内联源

回答by Rob

Using the AddEmbeddedImage() function works well in showing the embedded image in the web based emails. However, Yahoo always adds it as an attachment too. To overcome this problem, you can safely disregard the AddEmbeddedImage() and link to the full path of the image on your server and PHPMailer has the capacity of converting it to CID and It will correctly show as embedded image and Yahoo won't add it as an attachment anymore.

使用 AddEmbeddedImage() 函数可以很好地显示基于 Web 的电子邮件中的嵌入图像。但是,雅虎也总是将其添加为附件。为了克服这个问题,您可以安全地忽略 AddEmbeddedImage() 并链接到您服务器上图像的完整路径,PHPMailer 可以将其转换为 CID 并且它将正确显示为嵌入图像,而雅虎不会添加它不再作为附件。

In the html body of the message, add it like usual:

在消息的 html 正文中,像往常一样添加:

<img src="http://PATH-TO-IMAGE" alt='THIS IS THE IMAGE" />

<img src="http://PATH-TO-IMAGE" alt='THIS IS THE IMAGE" />