解决163/sohu/sina不能够收到PHP MAIL函数发出邮件的问题

yipeiwu_com6年前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 it
mail($to, $subject, $message, $headers);

查看sendmail的maillog,发现奇怪的内容。
复制代码 代码如下:

Mar 1 11:28:03 <a title="shaohui" href="http://www.shaohui.org" target="_blank">shaohui</a>.org sendmail[27526]: n213S1Xc027524: to=<shaohui_1983@163.com>, ctladdr=<shaohui@shaohui.org> (500/500), delay=00:00:02, xdelay=00:00:01, mailer=esmtp, pri=150812, relay=163mx03.mxmail.netease.com. [220.181.12.72], dsn=5.0.0, stat=Service unavailable

但是,如果我使用Linux Shell 的mail命令是可以发送成功的,不过多加了一条-f 参数伪造发件人。这是唯一的不同,于是maillog 的其中一个字段ctladdr显示也不一样。不再是apache用户,我怀疑163等国内的邮件服务提供商,把所有的apache的用户的邮件当成垃圾邮件处理掉了。
复制代码 代码如下:

Feb 25 23:44:59 <a title="shaohui" href="http://www.shaohui.org" target="_blank">shaohui</a> sendmail[13067]: n1PFixH4013067: to=shaohui_1983@163.com, ctladdr=contact@shaohui.org (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30869, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (n1PFixdx013068 Message accepted for delivery)

根源找到,于是问题就很好解决了,查一下php的手册,发现mail函数原来也是可以伪造发件人的。
复制代码 代码如下:

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

在第六个参数additional_parameters使用额外的参数"-f sender_addr@mydomain.com", 问题就解决了。

相关文章

50个PHP程序性能优化的方法

1、 用单引号代替双引号来包含字符串,这样做会更快一些。因为 PHP 会在双引号包围的 字符串中搜寻变量,单引号则不会,注意:只有 echo 能这么做,它是一种可以把多个字符 串当作参数...

php实现将HTML页面转换成word并且保存的方法

本文实例讲述了php实现将HTML页面转换成word并且保存的方法。分享给大家供大家参考,具体如下: 这里用使用到一个PHP的工具叫:PHPWord。 生成Word的原理是,将堆规定好了...

Mac下php 5升级到php 7的步骤详解

前言 在MAC OS X 10.11中php的版本是5.5的,近来一年多里,看到了很多关于php7介绍,以为php7增加了很多新特性,也删除了原来很多的老特性,所以一直以来并没想去尝试使...

解析PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思

PHP现在推出5.3.0版本了,不过下载的时候有几个不同版本选择。那就是VC6 X86和VC9 X86。首先我来解答: VC6是什么?VC6就是legacy Visual Studio...

PHP7中I/O模型内核剖析详解

PHP7中I/O模型内核剖析详解

1.同步:我客户端(C端调用者)一个功能,该功能没有结束前,我死等结果。 2.异步:我(c端调用者)调用一个功能,不知道该功能结果,该功能有结果后通知我,即回调通知 3.阻塞:就是调用我...