php - Outlook 接收 PHP HTML 电子邮件作为代码?

标签 php html outlook-2003

我用 PHP 发送了一封 HTML 电子邮件,但我的客户收到的是纯代码。这是发送邮件的 PHP 代码:

$subject = 'HTML e-mail test';
$message = '<html>
    <body>
        <h1>TEST</h1>
    </body>
</html>';
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: <".$to.">\r\n"; 
$headers .= "From: <".$email.">\r\n";

$mailb = mail($to, $subject, $message, $headers);

它对我来说很好用,但他们收到的是:

Content-type: text/html; charset=iso-8859-1

To: <email@email.com>
From: <email@email.com>

<html>
    <body>
        <h1>TEST</h1>
    </body>
</html>

我的标题有问题吗?或者是他们的 Outlook? 我该如何解决这个问题?
提前致谢!

最佳答案

我明白了:

Content-typetext/html; charset=iso-8859-1

我应该看到的地方:

Content-type: text/html; charset=iso-8859-1

这是剪切/粘贴错误吗?在您的代码中,还是在您的结果中?

请注意,您可能希望在多部分邮件中同时包含 text/plain 和 text/html 类型,因为纯 HTML 邮件通常会获得较高的垃圾邮件分数(使用 SpamAssassin、SpamBouncer 等)。

更新 #1:

\r\n 似乎被解释为两个换行符而不是一个。不同的平台在 SMTP 的实现中可能有不同的错误。您可以将行结尾更改为 \n。如果它适用于您的系统,请不要依赖它,因为它可能不适用于其他系统。

另外,请考虑切换到另一种发送邮件的方法。 phpMailerSwiftMailer推荐使用 PHP 的内部 mail() 函数。

更新 #2:

根据 Incognito 的建议,您可以使用以下代码更好地组织标题,以及创建纯文本部分:

filter_var($to, FILTER_VALIDATE_EMAIL) or die("Invalid To address");
filter_var($email, FILTER_VALIDATE_EMAIL) or die("Invalid From address");

$subject = 'HTML e-mail test';

$messagetext = 'TEST in TEXT';

$messagehtml = '<html>
    <body>
        <h1>TEST</h1>
        <p>in HTML</p>
    </body>
</html>';

// We don't need real randomness here, it's just a MIME boundary.
$boundary="_boundary_" . str_shuffle(md5(time()));

// An array of headers. Note that it's up to YOU to insure they are correct.
// Personally, I don't care whether they're a string or an imploded array,
// as long as you do input validation.
$headers=array(
    'From: <' . $email . '>',
    'MIME-Version: 1.0',
    'Content-type: multipart/alternative; boundary="' . $boundary . '"',
);

// Each MIME section fits in $sectionfmt.
$sectionfmt = "--" . $boundary . "\r\n"
      . "Content-type: text/%s; charset=iso-8859-1\r\n"
      . "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
      . "%s\n";

$body = "This is a multipart message.\r\n\r\n"
      . sprintf($sectionfmt, "html", $messagehtml)
      . sprintf($sectionfmt, "plain", $messagetext)
      . "--" . $boundary . "--\r\n";

$mailb = mail($to, $subject, $body, implode("\r\n", $headers));

我并不是说这是以任何方式做到这一点的正确方法,但如果该策略对您有吸引力,并且您认为在 6 或 12 个月没有查看代码(即乍一看很有意义),然后随意使用或改编它。

免责声明:这是未经测试的,有时我会打错字……只是为了让人们保持警惕。 :)

关于php - Outlook 接收 PHP HTML 电子邮件作为代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12072470/

相关文章:

c# - Outlook 2003 加载项无法加载,但处于工作状态

php - 如何使用 MySQL 中的数据填充复选框?

php - Zend 框架上的 CSS

jQuery UI 可拖动 : get the underlaying element at drag-stop

javascript - 为什么 JQuery 脚本不删除表行

visual-studio-2010 - 哪个 Microsoft.Office.Interop.Outlook 版本可与 Outlook 2003 配合使用?

php - 上传图片到服务器时报错500

php - 更改 Woocommerce 结帐端点以显示订单摘要详细信息

html - ajax登录使用httpRequest?