2011-04-19 37 views
7

嗨, 尝试通过邮件()发送HTML邮件,但是Gmail中只显示电子邮件作为纯文本,无标记,如HTML格式的邮件:如何发送使用PHP

mail("[email protected]", "subject", "<i>Italic Text</i>"); 

只是显示为

<i>Italic Text</i> 

任何想法?

+0

可能的重复[在PHP发送的电子邮件中使用HTML格式](http://stackoverflow.com/questions/4971322/use-html-formatting-in-email-sent-from-php) – mario 2011-04-19 11:09:49

+0

可能的重复[ Php mail:如何发送html?](http://stackoverflow.com/questions/4897215/php-mail-how-to-send-html) – mario 2011-04-19 11:10:37

+0

[Include html in email](http:// stackoverflow .com/questions/4916477/include-html-in-email) – mario 2011-04-19 11:11:09

回答

15

您必须指定邮件的内容是HTML:

mail("[email protected]", "subject", "<i>Italic Text</i>", "Content-type: text/html; charset=iso-8859-1"); 
3

你需要有一个正确的HTML文档:

$msg = "<html><head></head><body><i>Italic Text</i></body></html>"; 

编辑:

和发送标题:

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

mail("[email protected]", $msg, $headers); 
1

为了使其得到适当的解释,还必须设置的标头中的电子邮件,尤其是:

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

你还应该设置其他常规头。该消息最终可以如下发送:

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

有关更多信息,请参阅php manual mail page