今天想通过php发送邮件,
根据手册描述,发送html邮件需要指定header头
1 | <?php |
2 | $header = "MIME-Version: 1.0\r\n" ; |
3 | $header .= "From:admin@xxx.com\r\n" ; |
4 | $header .= "Content-Type:text/html;charset=utf-8\r\n" ; |
5 | $message = "<html><body><a href='http://xxx.com'></a></body></html>" ; |
6 | mail( 'cxz@163.com' , '测试邮件标题' , $message , '-fadmin@xxx.com' ); |
结果发出的邮件还是显示源代码,html并未生效,连header头信息都当作文本显示出来了。
以为是postfix的原因,百度了一通,无解。
goole了一下,发现有老外的文章提到这个问题,解决办法是:
header头信息末尾不要用 \r\n,用PHP_EOL替代,如下:
1 | $header = "MIME-Version: 1.0" .PHP_EOL; |
2 | $header .= "From:admin@xxx.com" .PHP_EOL; |
3 | $header .= "Content-Type:text/html;charset=utf-8" .PHP_EOL; |
手册里有提到:
If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.