php AddEmbeddedImage() 函数嵌入内嵌图像以及附加与附件相同的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18445525/
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 17:35:37  来源:igfitidea点击:

AddEmbeddedImage() function embadding inline images as well as attaching same image as attachement

phphtmlemail

提问by Sumit Tawal

I have added following parameters to PHPMailer object. Though I have embedded images for inline purpose using AddEmbeddedImage() function, it is working as expected, but additionally attaching same images as attachment to email & displaying at bottom.

我已将以下参数添加到 PHPMailer 对象。虽然我使用 AddEmbeddedImage() 函数嵌入了用于内联目的的图像,但它按预期工作,但另外将相同的图像作为附件附加到电子邮件并显示在底部。

$msg = `<table><tr><td colspan="2"><img  src="cid:header_jpg" alt="www.example.in" width="770" height="4" border="0" /></td></tr></table>`;

$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth   = false;        // enable SMTP authentication
$mail->Port       = 25;           // set the SMTP server port
$mail->Host       = 'localhost';  // SMTP server
$mail->Username   = "";           // SMTP server username
$mail->Password   = "";           // SMTP server password

$mail->AddReplyTo($sender, $sender_name);

$mail->From       = $sender;
$mail->FromName   = $sender_name;

$mail->AddAddress($receiver);

$mail->Subject  = $subject;

//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 80; // set word wrap

$mail->MsgHTML($msg);

$mail->IsHTML(true); // send as HTML
$mail->AddEmbeddedImage('./images/header.jpg', 'header_jpg');          
$mail->AddEmbeddedImage('./images/logo.jpg', 'logo_jpg');        
$mail->AddEmbeddedImage('./images/alert_icon.png', 'alert_icon_png', 'alert_icon.png');        
$mail->Send();

Please suggest something as early as possible...

请尽早提出建议...

回答by Rob

I am having the same issue with web email embedded images. I tried different approaches and got these results:

我在使用 Web 电子邮件嵌入图像时遇到了同样的问题。我尝试了不同的方法并得到了这些结果:

Sending html email to Yahoo:

向雅虎发送 html 电子邮件:

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png", "base64", "image/png");

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png", "base64", "image/png");

OR

或者

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png", "base64", "application/octet-stream");

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png", "base64", "application/octet-stream");

OR

或者

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png");

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png");

Same results; Yahoo showed the embedded image correctly but still attached it too!

结果相同;雅虎正确显示了嵌入的图像,但仍然附加了它!

With hotmail, it correctly embedded the image and no attachments added.

使用 hotmail,它正确嵌入了图像并且没有添加附件。

Finally, I fount that PHPMailer has the ability to automatically embed images from your HTML email. You have to put the full path in the file system, when writing your HTML email. I ended up disregarding the AddEmbeddedImageand link the source of the image directly to its location on the website. It worked correctly in both Hotmail and Yahoo and no attachment was added in Yahoo.

最后,我发现 PHPMailer 能够从您的 HTML 电子邮件中自动嵌入图像。在编写 HTML 电子邮件时,您必须将完整路径放在文件系统中。我最终忽略了 AddEmbeddedImage并将图像源直接链接到它在网站上的位置。它在 Hotmail 和 Yahoo 中都可以正常工作,并且没有在 Yahoo 中添加附件。

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

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

Needless to say, that embedded images in emails may not immediately show unless the user clicks the "show images" button; it all depends on their privacy & security settings.

不用说,除非用户单击“显示图像”按钮,否则电子邮件中的嵌入图像可能不会立即显示;这一切都取决于他们的隐私和安全设置。

I hope this helps!

我希望这有帮助!

回答by John Bishop

IMHO AddEmbeddedImageworks just fine. The problem above is that the first parameter that points to the image file must specify the absolute host filepath name (aka URI), not the URL. On my host that resolves to '/home/myaccountid/public_html/image_filename.jpg'. As a URL, that becomes 'http://myhostid.com/image_filename.jpg'.

恕我直言AddEmbeddedImage工作得很好。上面的问题是指向图像文件的第一个参数必须指定绝对主机文件路径名(又名 URI),而不是 URL。在我的主机解析为一个“/ home / myaccountid/public_html/image_filename.jpg”。作为 URL,它变成了“ http://myhostid.com/image_filename.jpg”。

回答by Cezco

Here attach code :

这里附上代码:

$mail->AddEmbeddedImage('img/logo.png', 'logoimg', 'xx.png', 'base64', 'image/png');

Then you can do this in message body :

然后您可以在消息正文中执行此操作:

<tr>
<td>Picture Attach</td>
<td><img  src='cid:logoimg' alt='OnlyPict' border='0' width='30%' height='5'/></td>
</tr>