使用msmtp通过终端发送邮件可以正常工作,但不适用于php mail() [英] Sending mail through terminal using msmtp works fine, but doesn't work with php mail()

查看:306
本文介绍了使用msmtp通过终端发送邮件可以正常工作,但不适用于php mail()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在运行kubuntu桌面的ubuntu 12.04服务器上安装了msmtp。 msmtp的安装工作正常,我什至可以使用gmail的smtp服务器通过终端发送邮件。

Recently I installed msmtp on my ubuntu 12.04 server that is running the kubuntu desktop. The install of msmtp worked fine, and I can even send mail through the terminal using gmail's smtp server.

我的问题是,尽管通过终端发送邮件有效,但它仍然不适用于php邮件。我曾尝试使用php mail()函数,但从未收到任何电子邮件。

My problem is that although sending mail works through the terminal, it still doesn't work with php mail. I have tried using the php mail() function, but I never receive any email.

我检查了apache错误日志(/var/log/apache2/error.log),但它们为空。

有人知道吗如何解决这个问题?如果需要任何进一步的信息,请这样说:)

I have checked the apache error logs (/var/log/apache2/error.log), but they are empty.
Does anyone know how to fix this? If any further information is needed, just say so :)

此外,在执行php脚本时,不会出现任何错误。

Also when executing the php script, no errors appear.

以下代码回显了已发送的邮件,但我从未收到电子邮件:

The code below echos Mail Sent, but I never recieve an email:

<?
if(mail( 'noreply.njmedia@gmail.com', 'Test mail from localhost', 'Working Fine.'))
{
    echo 'Mail sent';
}
else
{
    echo 'Error. Please check error log.';
}
?>


推荐答案

对我来说,原因是文件所有权不正确&

For me the cause was incorrect file ownership & permissions on the msmtp config file.

当我尝试从CLI以 root ,它起作用了。但是,将用户切换到 www-data 并尝试再次运行脚本(也在CLI上)失败,并显示以下消息:

When I tried to run the PHP mail-sending script from the CLI as root, it worked. However switching user to www-data and trying to run the script again (also on CLI) failed with the following messages:

msmtp: /etc/msmtprc: contains secrets and therefore must be owned by you
msmtp: /etc/msmtprc: contains secrets and therefore must have no more than user read/write permissions

通过对Apache(或Nginx等)的HTTP请求触发PHP脚本将具有结果相同(因为msmtp将由 www-data 用户调用)。

Triggering the PHP script by a HTTP request to Apache (or Nginx etc) would have the same results (since msmtp would be invoked by the www-data user).

假定您的msmtp配置文件为在 / etc / msmtprc 中,这些命令将解决以下问题:

Assuming your msmtp config file is at /etc/msmtprc, these commands would fix those issues:

chown www-data:www-data /etc/msmtprc
chmod 600 /etc/msmtprc

进行这些更改之前,请考虑这些新权限是否适合您的情况(例如,是否存在安全隐患?)。

Before making these changes please consider if these new permissions are appropriate for your circumstances (eg. are there security implications?).

这篇关于使用msmtp通过终端发送邮件可以正常工作,但不适用于php mail()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆