php - 为什么 html 标 checkout 现在我的 PHP 字符串中?

标签 php email header html-email

我正在使用一个邮件函数,它发回一条包含在变量 $body 中的消息。我想用某些粗体字和一些中断标记将消息发送给自己。

这是我的代码(HEREDOC SYNTAX):

$body = <<<CTS
<h1><b>Order for $name on datevariable</b></h1><br /><br />

<b><u>Administrative Details</u></b><br />
<b>Employee ID:</b> $id<br />
<b>Cost Center:</b> $cost_center<br />
<b>Approved By:</b> $approved_by<br />
<b>Delivery Bldg:</b> $delivery_bldg<br />
<b>Delivery Contact Email:</b> $delivery_contact<br />
<b>Ext:</b> $del_contact_ext<br />
CTS;

出于某种原因,当我收到电子邮件时,它看起来像这样:

<h1><b>Order for James Important on datevariable</b></h1><br /><br />
<b><u>Administrative Details</u></b><br />
<b>Employee ID:</b> 213123<br />
<b>Cost Center:</b> 132123<br />
<b>Approved By:</b> Chris Seckla<br />
<b>Delivery Bldg:</b> 6<br />
<b>Delivery Contact Email:</b> test@email.com<br />
<b>Ext:</b> 56<br />

它填充了变量值,但出于某种原因忽略了 html 标签。有人可以告诉我如何解决这个问题吗?

此外,它会忽略中断标记,仅在我留下一行空白时才进行中断。对此有什么想法吗?

最佳答案

如果这是一封电子邮件,您必须将 mime 类型设置为 text/html 而不是 text/plain。否则您的电子邮件阅读器会将邮件解释为纯文本并忽略所有 html 标签。

例如,假设您正在这样调用 mail 函数:

mail( $to, $subject, $message, $headers )

$headers 中,您需要遵循这些原则(当然会针对您的特定情况进行调整):

$headers = "From: webmaster@example.com\r\n";
$headers .= "Reply-To: webmaster@example.com\r\n";
$headers .= "Content-Type: text/html";

如果您想发送纯文本和 html 电子邮件(这样非 html 读者可以看到没有所有这些标签的干净版本的电子邮件),我建议您查看 this有关多部分电子邮件的更详细说明的教程。

关于php - 为什么 html 标 checkout 现在我的 PHP 字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/913793/

相关文章:

delphi - 如何更改 Delphi Firemonkey XE7 中 Stringgrid 标题的字体大小?

javascript - 使用 jquery 文件上传保持文件顺序

php - 在产品 View 页面中向 friend 发送电子邮件不起作用

linux - 从 crontab 发送邮件时,电子邮件正文内容被删除

php - 如何使用python向多个收件人发送电子邮件?

html - 说明 "Cache-Control" header 的用法

c - 将 C 中的 '#define Version x.x' 与 Git 中的版本标记链接起来

php - Wordpress:回显当前帖子永久链接

javascript - 将jQuery变量传递给PHP以进行错误处理

php - Propel 实例池上元素的 "Duration"