PHP mail() 从命令行工作,但不是 apache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1462941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mail() works from command line but not apache
提问by matt
I'm trying to figure out why the mail function in PHP fails when called via web browser (i.e. apache), but I can run the same script from the command line using
我试图弄清楚为什么 PHP 中的邮件功能在通过 Web 浏览器(即 apache)调用时失败,但我可以使用从命令行运行相同的脚本
php -f mailtest.php
php -f 邮件测试.php
This is one of my client's Fedora servers, so I don't grok it completely, but I do have root access should I need to change anything.
这是我客户的 Fedora 服务器之一,所以我没有完全理解它,但是如果我需要更改任何内容,我确实拥有 root 访问权限。
from php.ini:
来自 php.ini:
sendmail_path = /usr/sbin/sendmail -t -i
sendmail_path = /usr/sbin/sendmail -t -i
Not sure if this could matter, but /usr/sbin/sendmail is a symlink to /etc/alternatives/mta, which is a symlink back to /usr/sbin/sendmail.sendmail. FWIW the apache user does have permission to run sendmail (tested sendmail directly from the command line).
不确定这是否重要,但 /usr/sbin/sendmail 是指向 /etc/alternatives/mta 的符号链接,它是指向 /usr/sbin/sendmail.sendmail 的符号链接。FWIW apache 用户确实有权运行sendmail(直接从命令行测试sendmail)。
OS: Fedora Core 7 Linux (kernel 2.6.23.17)
Apache: 2.2.8
PHP: 5.2.6
Any help here will be greatly appreciated!
这里的任何帮助将不胜感激!
回答by matt
I found the problem. SELinux was preventing apache from being able to use sendmail. To diagnose, I used
我发现了问题。SELinux 阻止了 apache 能够使用 sendmail。为了诊断,我用
$ sestatus -b | grep sendmail
httpd_can_sendmail off
Then to actually fix the problem:
然后实际解决问题:
$ restorecon /usr/sbin/sendmail
$ setsebool -P httpd_can_sendmail 1
Read more about it here.
在此处阅读更多相关信息。
回答by Josh
Anything in apache's error_log? Is PHP being run as an apache module or a CGI binary?
apache 的 error_log 中有什么吗?PHP 是作为 apache 模块还是 CGI 二进制文件运行?
EDIT: Hmmm... nothing in the error log. What does the call to mail(...)return? Anything interesting in the mail log? This will vary depending on the MTA, often /var/log/maillog
编辑:嗯......错误日志中没有任何内容。mail(...)返回的调用是什么?邮件日志中有什么有趣的事情吗?这取决于 MTA,通常是 /var/log/maillog
EDIT 2: Is safe_modeturned on and are you using the mail()function's additional_parameters?
编辑 2:已safe_mode打开并且您是否正在使用该mail()函数的additional_parameters?
回答by Rob Drimmie
Is it a user permissions error? Your account and the one used to execute PHP scripts may have different privileges.
是用户权限错误吗?您的帐户和用于执行 PHP 脚本的帐户可能具有不同的权限。
回答by Alexandre
This is my first answer here on StackOverflow! :o
这是我在 StackOverflow 上的第一个答案!:o
So I had the same issue than you, matt! I use OpenSuse. I figured out that postfix checkresulted with
所以我和你有同样的问题,马特!我使用 OpenSuse。我发现postfix check结果是
postfix/postfix-script: warning: not owned by group maildrop: /usr/sbin/postqueue
postfix/postfix-script: warning: not owned by group maildrop: /usr/sbin/postdrop
postfix/postfix-script: warning: not set-gid or not owner+group+world executable: /usr/sbin/postqueue
postfix/postfix-script: warning: not set-gid or not owner+group+world executable: /usr/sbin/postdrop
so I ran the next commands:
所以我运行了下一个命令:
# my postfix user is postfix and postfix group is maildrop
sudo chown 'postfix:maildrop' /usr/sbin/post{drop,queue}
sudo chmod g+s /usr/sbin/post{queue,drop}
and then, I tried to simple PHP script from my browser to test if everything works fine: (assuming you want to mail [email protected])
然后,我尝试从我的浏览器中编写简单的 PHP 脚本来测试一切是否正常:(假设您想发送邮件到 [email protected])
<?php
$ret = mail('[email protected]', 'subject', 'message');
if ($ret === true)
echo 'Success'.PHP_EOL;
else
echo 'Error'.PHP_EOL;
and that's fine! I hope you will fix the issue with this method
没关系!我希望你能用这个方法解决这个问题

