2168575 发表于 2015-9-15 11:07:10

php mail函数发送html邮件不解析,linux+postfix

  今天想通过php发送邮件,
  根据手册描述,发送html邮件需要指定header头



<?php
$header="MIME-Version: 1.0\r\n";
$header.="From:admin@xxx.com\r\n";
$header.="Content-Type:text/html;charset=utf-8\r\n";
$message="<html><body><a href='http://xxx.com'></a></body></html>";
mail('cxz@163.com','测试邮件标题',$message,'-fadmin@xxx.com');
  
  结果发出的邮件还是显示源代码,html并未生效,连header头信息都当作文本显示出来了。
  以为是postfix的原因,百度了一通,无解。
  goole了一下,发现有老外的文章提到这个问题,解决办法是:
  header头信息末尾不要用 \r\n,用PHP_EOL替代,如下:
  



$header="MIME-Version: 1.0".PHP_EOL;
$header.="From:admin@xxx.com".PHP_EOL;
$header.="Content-Type:text/html;charset=utf-8".PHP_EOL;
  原因,php下换行符是\n,win下换行符是\r\n,这里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 &raquo;
页: [1]
查看完整版本: php mail函数发送html邮件不解析,linux+postfix