【问题标题】:Email clients not seeing plain text alternative of HTML email电子邮件客户端看不到 HTML 电子邮件的纯文本替代品
【发布时间】:2013-02-07 23:25:51
【问题描述】:

我相信我正在发送格式正确的多部分/替代 MIME 电子邮件,其中包含不同的纯文本和 HTML 版本的内容。但是,一旦收到电子邮件客户端(我尝试了 3 种不同的),电子邮件的来源似乎已更改;使用了不同的边界文本,并且纯文本内容已更改为 HTML 内容(去除了 HTML 标记)。

这是我的 PHP 脚本:

<?php
$boundary = md5(date('r', time())); 
$to = '$to_email_address';
$subject = 'Test HTML email'; 
$headers =  "From: $from_email_address\r\n" .
            "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\""; 
$message = "--" . $boundary . "\r\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . 
            "Content-Transfer-Encoding: 7bit\r\n" .
            "\r\n" .
            "Hello World!!!\r\n" .
            "This is simple text email message.\r\n" .
            "\r\n" .
            "--" . $boundary . "\r\n" . 
            "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" . 
            "Content-Transfer-Encoding: 7bit\r\n" .
            "\r\n" .
            "<h2>Hello World!</h2>\r\n" .
            "<p>This is something with <b>HTML</b> formatting.</p>\r\n" .
            "\r\n" .
            "--" . $boundary . "--\r\n";

$result = mail($to, $subject, $message, $headers);
echo $result ? "Mail sent" : "Mail failed";
?>

这是电子邮件客户端看到的电子邮件原始来源:

Return-Path: <from_email_address>
From: <from_email_address>
To: <to_email_address>
Subject: Test HTML email
Date: Fri, 22 Feb 2013 10:22:30 -0000
Message-ID: <01d001ce10e6$8c41d450$a4c57cf0$@com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="----=_NextPart_000_01CC_01CE10E6.8C41D450"
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: Ac4Q5obGqVqbDjA7SDSMnVZDykxxhA==
X-OlkEid: EE843820EF8935D69CB57C40B3E9F64C8874FA01

This is a multi-part message in MIME format.

------=_NextPart_000_01CC_01CE10E6.8C41D450
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


Hello World!


This is something with HTML formatting.


------=_NextPart_000_01CC_01CE10E6.8C41D450
Content-Type: text/html;
    boundary="b04d8008be2af4ce2409beb4e26761bd";
    charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

------=_NextPart_000_01CC_01CE10E6.8C41D450--

如何确保接收客户端收到预期的纯文本内容?

提前感谢您的帮助, 斯图尔特

【问题讨论】:

  • 第二个正文部分的Content-Type 标头不需要边界参数。

标签: php html-email mime email-client plaintext


【解决方案1】:

我想我找到了问题所在。我发送的很好。但是,我随后使用 Microsoft Outlook 在电子邮件帐户之间移动收到的电子邮件,以便使用不同的电子邮件客户端查看电子邮件源(因为您无法在 Outlook 中查看电子邮件的原始源),而 Outlook 在此过程中更改了我的电子邮件。嗬!小学生错误!

斯图尔特

【讨论】:

  • 是的,一定喜欢 Outlook - 它对开发人员非常友好。那么最好接受你自己的答案。
【解决方案2】:

用于支持邮件中的 html 内容。您必须在标题中添加以下内容,

$header .="Content-Type: text/html; charset=ISO-8859-1\r\n";

在你的代码中,

"Content-Type: multipart/alternative"

【讨论】:

  • 我认为这不对。整个电子邮件的 Content-Type 是“multipart/alternative”,表示电子邮件将包含相同内容的多个版本。然后我继续定义两个子类型,第一个是 text/plain 类型,第二个是 text/html 类型。收到后,两个版本都可用。 HTML 看起来不错,但我只是没有看到我指定的纯文本版本(我看到了从 HTML 派生的纯文本版本)
猜你喜欢
  • 2010-09-25
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 2018-07-04
  • 2015-09-25
  • 2022-11-20
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多