[英]Why are the html tags showing up in my string in PHP?
I'm using a mail function which is sending back a message that is contained within the variable $body. 我正在使用一个邮件功能,它发回一个包含在变量$ body中的消息。 I want to send the message to myself with certain words in bold and a number of break tags.
我希望用一些粗体字和一些中断标记将消息发送给自己。
Here is my code(HEREDOC SYNTAX): 这是我的代码(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;
For some reason when I receive the email, it looks like this: 出于某种原因,当我收到电子邮件时,它看起来像这样:
<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 />
It fills in the variable values but for some reason ignores the html tags. 它填充变量值但由于某种原因忽略了html标记。 Can someone please tell me how to fix this?
有人可以告诉我如何解决这个问题吗?
Also, it is ignoring the break tags and only putting breaks when I leave a line of whitespace. 此外,它忽略了break标记,只留下了一行空格。 Any ideas on this?
有什么想法吗?
If this is an email, you have to set the mime type to text/html instead of text/plain. 如果这是一封电子邮件,则必须将mime类型设置为text / html而不是text / plain。 Otherwise your email reader will interpret the message as plain text and ignore all the html tags.
否则,您的电子邮件阅读器会将邮件解释为纯文本,并忽略所有html标记。
For example, lets say you are calling the mail
function like this: 例如,假设您正在调用这样的
mail
功能:
mail( $to, $subject, $message, $headers )
In $headers
, you'd need something along these lines (adjusted for your particular case, of course): 在
$headers
,你需要这些内容(当然根据你的具体情况进行调整):
$headers = "From: webmaster@example.com\r\n";
$headers .= "Reply-To: webmaster@example.com\r\n";
$headers .= "Content-Type: text/html";
If you want to send both plain text and html email (so non-html readers can see a clean version of the email without all those tags), I would suggest taking a look at this tutorial for a more detailed explanation of multipart emails. 如果你想发送纯文本和HTML电子邮件(所以非HTML阅读器可以看到没有所有这些标签的电子邮件的干净版本),我建议看一下本教程,以获得有关多部分电子邮件的更详细说明。
You have to set an additional header when sending from php for the email to display using html format: 从php发送电子邮件时,您必须设置一个额外的标题,以便使用html格式显示:
Content-Type: text/html; 内容类型:text / html; charset=whatever
字符集=什么
Make sure you send the header that states it is HTML... 确保发送标题为HTML的标题...
example: 例:
mail('email@domain.com', 'Subject',
'<html><body><b>Hello</b></body></html>',
"To: The Receiver <recipient@domain.com>\n" .
"From: The Sender <email@domain.com>\n" .
"MIME-Version: 1.0\n" .
"Content-type: text/html; charset=iso-8859-1");
如果您想使用PHP发送邮件,我建议您使用PEAR Mail_Mime包而不是普通邮件功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.