[英]PHP mail() works from command line but not apache
I'm trying to figure out why the mail function in PHP fails when called via web browser (ie apache), but I can run the same script from the command line using 我试图弄清楚为什么PHP中的邮件功能在通过Web浏览器(即apache)调用时失败,但我可以从命令行运行相同的脚本
php -f mailtest.php
php -f mailtest.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. 不确定这是否重要,但是/ usr / sbin / sendmail是/ etc / alternatives / mta的符号链接,这是一个符号链接回/usr/sbin/sendmail.sendmail。 FWIW the apache user does have permission to run sendmail (tested sendmail directly from the command line).
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! 任何帮助将非常感谢!
I found the problem. 我发现了这个问题。 SELinux was preventing apache from being able to use sendmail.
SELinux阻止了apache能够使用sendmail。 To diagnose, I used
诊断,我用过
$ sestatus -b | grep sendmail
httpd_can_sendmail off
Then to actually fix the problem: 然后实际解决问题:
$ restorecon /usr/sbin/sendmail
$ setsebool -P httpd_can_sendmail 1
Anything in apache's error_log? 在apache的error_log中有什么东西吗? Is PHP being run as an apache module or a CGI binary?
PHP是作为apache模块还是CGI二进制文件运行的?
EDIT: Hmmm... nothing in the error log. 编辑:嗯...错误日志中没有任何内容。 What does the call to
mail(...)
return? mail(...)
回叫是什么? Anything interesting in the mail log? 邮件日志中有什么有趣的东西? This will vary depending on the MTA, often /var/log/maillog
这取决于MTA,通常是/ var / log / maillog
EDIT 2: Is safe_mode
turned on and are you using the mail()
function's additional_parameters ? 编辑2:
safe_mode
是否已打开,您是否正在使用mail()
函数的additional_parameters ?
Is it a user permissions error? 这是用户权限错误吗? Your account and the one used to execute PHP scripts may have different privileges.
您的帐户和用于执行PHP脚本的帐户可能具有不同的权限。
This is my first answer here on StackOverflow! 这是我在StackOverflow上的第一个答案! :o
:○
So I had the same issue than you, matt! 所以我遇到了和你相同的问题,亚光! I use OpenSuse.
我使用OpenSuse。 I figured out that
postfix check
resulted with 我发现
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 abc@gmail.com) 然后,我尝试从我的浏览器中简单的PHP脚本来测试一切是否正常:(假设你想邮寄abc@gmail.com)
<?php
$ret = mail('abc@gmail.com', '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
我希望你能解决这个方法的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.