我可以阻止 MIME 嵌入的 html 图像作为附件出现吗?

发布于 2024-12-01 00:14:42 字数 968 浏览 2 评论 0原文

这是我用来邮寄它的代码:

<?php
include('Mail.php');
include('Mail/mime.php');

$address = "Any old address will do";
$crlf = "\r\n";
$hdrs = array( 
    'From' => '[email protected]', 
    'Subject' => 'Mail_mime test message' 
    ); 

$mime = new Mail_mime($crlf); 

$mime->addHTMLImage("emailHeader.jpg", "image/jpg");

$cid=$mime->_html_images[0]['cid'];

$html = '<html><body><center><img src="cid:'.$cid.'">This image shows up just fine</center></body></html>';
$text = 'Plain text version of email';

$mime->setTXTBody($text);
$mime->setHTMLBody($html); 

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send($address, $hdrs, $body);

?>

图像显示在电子邮件中,但它也显示为附件。这有点笨拙,我可以阻止它吗?

Here is the code I am using to mail it:

<?php
include('Mail.php');
include('Mail/mime.php');

$address = "Any old address will do";
$crlf = "\r\n";
$hdrs = array( 
    'From' => '[email protected]', 
    'Subject' => 'Mail_mime test message' 
    ); 

$mime = new Mail_mime($crlf); 

$mime->addHTMLImage("emailHeader.jpg", "image/jpg");

$cid=$mime->_html_images[0]['cid'];

$html = '<html><body><center><img src="cid:'.$cid.'">This image shows up just fine</center></body></html>';
$text = 'Plain text version of email';

$mime->setTXTBody($text);
$mime->setHTMLBody($html); 

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send($address, $hdrs, $body);

?>

The image shows up in the email, but it is also shows up as an attachment. This is a bit clunky, can I prevent it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2024-12-08 00:14:42

我遇到一个问题,某些邮件客户端会将所有内联图像呈现为空框。

我发现,如果未提供domainID,PEAR Mail_mime 类将尝试为您修复Content-ID 引用。

    e.g.
    [HTML] src="123456.jpg" 
    [Headers] Content-ID: <123456.jpg>
    updates to
    [HTML] src="cid:[email protected]" 
    [Headers] Content-ID: <[email protected]>
    BUT
    [HTML] src="cid:123456.jpg" 
    [Headers] Content-ID: <123456.jpg>
    updates to
    [HTML] src="cid:123456.jpg" 
    [Headers] Content-ID: <[email protected]>

这会破坏 HTML 标记和 MIME 附件之间的链接。

这个答案帮助了我

因此,在发送电子邮件之前,将domainID 包含在Content-ID 中是最好的解决方案。

我循环发送了几封单独的电子邮件。每封电子邮件都应该是相同的,只是标题中的收件人在每次迭代时发生变化。我发现第一封电子邮件发送正确,然后在第二封及后续电子邮件中看到 Content-ID 的修改。

Outlook 中的初始测试并未发现该问题(图像正常)。仅在 Gmail 中进行测试才发现该故障。但是,如果 Gmail 检测到无效数据,它不会向您显示 src 属性,因此您无法仅通过 Gmail 中的电子邮件检查来发现问题。

I had an issue where some mail clients would render all inline images as empty boxes.

What I found out is that the PEAR Mail_mime class will attempt to fix your Content-ID references for you if a domainID is not provided.

    e.g.
    [HTML] src="123456.jpg" 
    [Headers] Content-ID: <123456.jpg>
    updates to
    [HTML] src="cid:[email protected]" 
    [Headers] Content-ID: <[email protected]>
    BUT
    [HTML] src="cid:123456.jpg" 
    [Headers] Content-ID: <123456.jpg>
    updates to
    [HTML] src="cid:123456.jpg" 
    [Headers] Content-ID: <[email protected]>

Which breaks the link between the HTML tag and the MIME attachment.

This answer helped me out

So including the domainID in the Content-ID yourself before sending the email is the best solution.

I was sending out several separate emails in a loop. Each email was meant to be the same with just the recipients in the header changing for each iteration. I found that the first email was sent correctly and then the munging of the Content-ID was seen on the second and subsequent emails.

Initial testing in Outlook did not uncover the issue (images were fine). Only testing in Gmail revealed the glitch. However Gmail will not show you the src attribute if it detects invalid data so you can't see the problem just with Inspect on the email in Gmail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文