不能发送邮件时,通过web服务器运行,但能够从命令行发送相同的php脚本


Not able to send mail when running through webserver but able to send from command line with same php script

我面临一个奇怪的问题:我在项目中有一个名为mail.php的文件。当我从shell中运行命令时:

php mail.php

以秒为单位发送邮件,但是当运行相同的文件时:

https://www.domain.com/mail.php

没有邮件发出,使用以下代码:

<?php
      echo "Sending mail now....";
      mail("mr.atanu.dey.83@gmail.com", "PHP Test mail", "Hope this works! ");
?>

有人能帮我吗?

可能是SELinux被启用的情况,根据SELinux的默认配置,apache/web服务器不允许发送邮件。

检查SELinux是否已启用,是否已配置为停止发送电子邮件,执行以下命令:

getsebool -a | grep mail
<标题> 的示例输出
allow_postfix_local_write_mail_spool --> on
httpd_can_sendmail --> off
logging_syslogd_can_sendmail --> off

现在,为了配置SELinux以允许apache发送电子邮件,运行以下命令:

setsebool -P httpd_can_sendmail on
检查这个答案,它解决了我的问题:不能通过web服务器发送邮件,但可以从命令行发送相同的php脚本

可能的解决方案:

在php.ini中替换

sendmail_path = /usr/sbin/sendmail -t -i

sendmail_path = /usr/sbin/sendmail.postfix -t -i