如何使用 PHP 发送 HTML 格式的邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5715357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I send HTML formatted mail using PHP
提问by DexCurl
Hey guys, trying to send a html email via mail(), however gmail just displays the email as plain text, no mark up, eg:
嘿伙计们,尝试通过 mail() 发送 html 电子邮件,但是 gmail 只是将电子邮件显示为纯文本,没有标记,例如:
mail("[email protected]", "subject", "<i>Italic Text</i>");
just appears as
只是显示为
<i>Italic Text</i>
Any ideas?
有任何想法吗?
回答by rael_kid
You have to specify that the contents of the mail is HTML:
您必须指定邮件的内容是 HTML:
mail("[email protected]", "subject", "<i>Italic Text</i>", "Content-type: text/html; charset=iso-8859-1");
回答by Daniel A. White
回答by Ant
You need to have a properly formed html doc:
您需要有一个格式正确的 html 文档:
$msg = "<html><head></head><body><i>Italic Text</i></body></html>";
Edit:
编辑:
And send headers:
并发送标头:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("[email protected]", $msg, $headers);
回答by bluefoot
I believe you must set the content-type
to text/html
in your headers.
我相信您必须在标题中设置content-type
to text/html
。
Something like the string "Content-type:text/html"
像字符串一样的东西 "Content-type:text/html"
Where to "insert" this header? Take a look at the function reference at http://php.net/manual/en/function.mail.php
在哪里“插入”这个标题?看看http://php.net/manual/en/function.mail.php的函数参考
回答by user123
In order for it to be properly interpreted, you must also set the headers in the email, especially:
为了正确解释它,您还必须在电子邮件中设置标题,尤其是:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
You should also set the other usual headers. The message can finally be sent like this:
您还应该设置其他常用标题。消息最终可以这样发送:
mail($to, $subject, $message, $headers);
You should refer to the php manual mail pagefor more information.
您应该参考php 手册邮件页面以获取更多信息。