我正在使用一个邮件函数,它发回一条包含在变量 $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/