使用 PEAR 的 Mail_Mime 时,附加 HTML 图像时文本部分不起作用

发布于 2024-11-09 14:47:25 字数 3871 浏览 5 评论 0 原文

我使用的是最新版本的 PEAR mail_mime 模块我遇到了一个奇怪的问题。将 HTML 图像附加到电子邮件时,文本部分将无法正常工作。我所说的“无法正常工作”是指,如果我尝试在 Thunderbird 中以文本形式查看消息(或在 PINE 中查看消息),它不会显示我设置为电子邮件“文本”部分的内容;它尝试以文本可读的方式解析 HTML 信息。 HTML 部分中的所有内容都按预期工作。

这是我的精简代码:

//Headers
$headers['From'] = "[email protected]";
$headers['To'] = "[email protected]";
$headers['Subject'] = "A MIME Email";

//Text Portion
$text = "This is the text portion.";

//HTML Portion
$html = "<html><body><p><img src='image.png' />This is the HTML portion.</p></body></html>";

//Set up the MIME email
$mime = new Mail_mime(array('eol' => "\r\n"));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addHTMLImage("/path/to/image.png", "image/png");

//Prepare the message to be sent
$body = $mime->get();
$headers = $mime->headers($headers);

//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'smtp.server.com', 'port' => 25));
$mail_obj->send("[email protected]", $headers, $body);

如果我注释掉 addHTMLImage 行,电子邮件的文本部分将按预期工作,并且我可以看到为非 HTML 视图设置的内容。但是,如果我取消注释该行,文本部分就会失败。查看我的消息来源,我可以看到文本部分始终存在。

以下是与此问题相关的消息源部分:

Date: Thu, 26 May 2011 10:39:24 -0500 (CDT)
From: From Address <[email protected]>
Subject: A MIME Email
To: [email protected]
Message-id: <[email protected]>
MIME-version: 1.0
Content-type: multipart/alternative;
 boundary="=_e107860f353617028a0059317ce51c1f"
Original-recipient: rfc822;[email protected]

--=_e107860f353617028a0059317ce51c1f
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
 charset=ISO-8859-1

This is the text portion.
--=_e107860f353617028a0059317ce51c1f
Content-Type: multipart/related;
 boundary="=_52fd7475fcf0abd310b6a38cd33f5c46"

--=_52fd7475fcf0abd310b6a38cd33f5c46
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
 charset=ISO-8859-1

<html><body><p><img src='cid:3c0db5d0a50b8752771f64048f527338' />This is th=
e HTML portion.</p></body></html>

--=_52fd7475fcf0abd310b6a38cd33f5c46
Content-Transfer-Encoding: base64
Content-ID: <3c0db5d0a50b8752771f64048f527338>
Content-Type: image/png;
 name=background.png
Content-Disposition: inline;
 filename=background.png

iVBORw0KGgoAAAANSUhEUgAAAAEAABOICAYAAABwnAfKAAAAkUlEQVR42u3WSwrAIAwFwND7X9d1
eoHSb9TSzmYQSV5AXCRaa7lEROySJ+8uFd+8q2l73FEdkJNSRgR07q0uuVncJWBY6I9OWy85bKRB
7xo04TN8/vTO0OriziWFvcMCJqTMCui8rj5fuyMz43i1BwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAIB/swItzir5J/ImiQAAAABJRU5ErkJggg==
--=_52fd7475fcf0abd310b6a38cd33f5c46--

--=_e107860f353617028a0059317ce51c1f--

我不知道如何解决此问题。有没有人遇到类似的问题或有什么建议?

I'm using the latest version of PEAR mail_mime module and I'm running into a strange issue. When attaching HTML images to the email, the text portion stops working properly. What I mean by "not work properly" is that if I try to view the message as text in Thunderbird (or view the message in PINE), it doesn't display what I have set as the "text" portion of the email; It tries to parse the HTML information in a text readable fashion. Everything in the HTML portion works as expected.

Here's my slimmed down code:

//Headers
$headers['From'] = "[email protected]";
$headers['To'] = "[email protected]";
$headers['Subject'] = "A MIME Email";

//Text Portion
$text = "This is the text portion.";

//HTML Portion
$html = "<html><body><p><img src='image.png' />This is the HTML portion.</p></body></html>";

//Set up the MIME email
$mime = new Mail_mime(array('eol' => "\r\n"));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addHTMLImage("/path/to/image.png", "image/png");

//Prepare the message to be sent
$body = $mime->get();
$headers = $mime->headers($headers);

//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'smtp.server.com', 'port' => 25));
$mail_obj->send("[email protected]", $headers, $body);

If I comment out the addHTMLImage line, the text portion of the email works as expected and I can see the content I've set for a non-HTML view. However, if I uncomment out the line, the text portion fails. Looking at the source of my message, I can see the text part is always there.

Here is the part of the message source that is relevant to this question:

Date: Thu, 26 May 2011 10:39:24 -0500 (CDT)
From: From Address <[email protected]>
Subject: A MIME Email
To: [email protected]
Message-id: <[email protected]>
MIME-version: 1.0
Content-type: multipart/alternative;
 boundary="=_e107860f353617028a0059317ce51c1f"
Original-recipient: rfc822;[email protected]

--=_e107860f353617028a0059317ce51c1f
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
 charset=ISO-8859-1

This is the text portion.
--=_e107860f353617028a0059317ce51c1f
Content-Type: multipart/related;
 boundary="=_52fd7475fcf0abd310b6a38cd33f5c46"

--=_52fd7475fcf0abd310b6a38cd33f5c46
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
 charset=ISO-8859-1

<html><body><p><img src='cid:3c0db5d0a50b8752771f64048f527338' />This is th=
e HTML portion.</p></body></html>

--=_52fd7475fcf0abd310b6a38cd33f5c46
Content-Transfer-Encoding: base64
Content-ID: <3c0db5d0a50b8752771f64048f527338>
Content-Type: image/png;
 name=background.png
Content-Disposition: inline;
 filename=background.png

iVBORw0KGgoAAAANSUhEUgAAAAEAABOICAYAAABwnAfKAAAAkUlEQVR42u3WSwrAIAwFwND7X9d1
eoHSb9TSzmYQSV5AXCRaa7lEROySJ+8uFd+8q2l73FEdkJNSRgR07q0uuVncJWBY6I9OWy85bKRB
7xo04TN8/vTO0OriziWFvcMCJqTMCui8rj5fuyMz43i1BwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAIB/swItzir5J/ImiQAAAABJRU5ErkJggg==
--=_52fd7475fcf0abd310b6a38cd33f5c46--

--=_e107860f353617028a0059317ce51c1f--

I'm at a loss on how to possibly fix this. Any body run into a similar issue or have any suggestions?

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

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

发布评论

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

评论(1

满地尘埃落定 2024-11-16 14:47:25

这是您的邮件阅读器。如果启用 HTML 邮件,它会隐藏文本邮件。在 Gmail 中,您可以查看详细信息,也可以在仅限文本电子邮件的阅读器中查看

it's your mail reader. it hides text mail if HTML mail is enabled. in gmail you can view details or you can check it in a text email only reader

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