代码之家  ›  专栏  ›  技术社区  ›  matt

php mail()可以从命令行运行,但不能从apache运行

  •  4
  • matt  · 技术社区  · 15 年前

    我试图弄清楚为什么通过Web浏览器(即Apache)调用PHP中的mail函数失败,但是我可以使用

    php-f mailtest.php

    这是我的客户机的Fedora服务器之一,因此我不会完全摸索它,但如果需要更改任何内容,我确实有根访问权。

    来自PHP.ini:

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

    不确定这是否重要,但是/usr/sbin/sendmail是指向/etc/alternates/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
    

    如有任何帮助,我们将不胜感激!

    4 回复  |  直到 7 年前
        1
  •  18
  •   zzapper    11 年前

    我发现了问题。Selinux阻止了Apache使用sendmail。为了诊断,我用

    $ sestatus -b | grep sendmail  
    httpd_can_sendmail                   off
    

    然后实际解决问题:

    $ restorecon /usr/sbin/sendmail
    $ setsebool -P httpd_can_sendmail 1
    

    了解更多信息 here .

        2
  •  0
  •   Rob Drimmie    15 年前

    这是用户权限错误吗?您的帐户和用于执行PHP脚本的帐户可能具有不同的权限。

        3
  •  0
  •   Josh Andreas Rehm    15 年前

    Apache的错误日志中有什么?PHP是作为Apache模块还是CGI二进制文件运行的?

    编辑:嗯…错误日志中没有任何内容。打电话给什么 mail(...) 返回?有什么有趣的邮件日志吗?这取决于MTA,通常是/var/log/maillog

    编辑2:IS safe_mode 打开并使用 mail() 函数的 附加\参数 ?

        4
  •  0
  •   Alexandre    8 年前

    这是我在StackOverflow上的第一个答案!o

    所以我和你有同样的问题,马特!我用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
    

    所以我运行了下面的命令:

    # 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}
    

    然后,我尝试从浏览器中简单地使用PHP脚本来测试是否一切正常:(假设您想发送abc@gmail.com)

    <?php
    $ret = mail('abc@gmail.com', 'subject', 'message');
    if ($ret === true)
      echo 'Success'.PHP_EOL;
    else
      echo 'Error'.PHP_EOL;
    

    很好!我希望你能用这种方法来解决这个问题