PHP的mail()函数发送邮件,其中的html标签未被正常解析的问题

来源:互联网 发布:杀毒清理软件 编辑:程序博客网 时间:2024/05/17 02:16

问题描述

最近有一台线上服务器迁移到了欧洲节点,服务器配置,操作系统和应用环境与之前都是一样,Centos6.5,PHP5.5,但是使用PHP自带的mail函数发送邮件,其中的html标签未被解析,被当成字符串直接显示出来,奇怪的是之前的服务器并没有这个问题。
这里写图片描述

问题分析

查看官方文档,mail函数的定义如下:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

通过官方文档的例子,不难看出要想发送html的邮件,关键在于第四个参数$additional_headers

<?php// multiple recipients$to  = 'aidan@example.com' . ', '; // note the comma$to .= 'wez@example.com';// subject$subject = 'Birthday Reminders for August';// message$message = '<html><head>  <title>Birthday Reminders for August</title></head><body>  <p>Here are the birthdays upcoming in August!</p>  <table>    <tr>      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>    </tr>    <tr>      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>    </tr>    <tr>      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>    </tr>  </table></body></html>';// To send HTML mail, the Content-type header must be set$headers  = 'MIME-Version: 1.0' . "\r\n";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";// Additional headers$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";// Mail itmail($to, $subject, $message, $headers);?>

使用这个例子在线上服务器上测试,仍然不行。但是在文档中发现了这样一个提示:

Note: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.

提示说,如果没有收到消息,尝试只用(\n),因为一些质量差的Unix邮件传输代理商自动将LF(\n)替换为CRLF(\r\n)(如果使用CRLF,则会导致出现两个CR)。

于是我将$headers中的所有的(\r\n)全部替换成(\n),再次发送邮件,HMTL标签全部显示正常。

0 0
原创粉丝点击