linux msmtp 配置从 shell 发送但从 PHP/apache 失败 [英] linux msmtp configuration sends from shell but fails from PHP/apache

查看:42
本文介绍了linux msmtp 配置从 shell 发送但从 PHP/apache 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

linux (fedora 20) msmtp 配置从 shell 发送但从 PHP/apache 失败,我很难过......我的目标只是通过我的本地开发网络服务器的 gmail smtp 发送电子邮件,以测试代码的输出发送邮件

php.ini sendmail 文件内容如下:sendmail_path =/usr/bin/msmtp --debug -C/etc/msmtprc --read-recipients

系统上只有一个 php.ini,用于 CLI 和位于/etc/php.ini 的网络服务器

/etc/msmtprc 的权限设置为 apache:apache 600

以 root 身份执行以下命令并生成测试电子邮件:

  • php -r "mail('emily@emilytench.net', '最新测试电子邮件', '测试电子邮件正文');"
  • runuser -l apache -c '/usr/bin/msmtp --debug -C/etc/msmtprc --read-recipients </var/www/html/test.mail'(test.mail 包括 to 和 from 行)

但是当从以下脚本调用 php 邮件函数时 apache/php 会产生错误:

if (mail('emily@emilytench.net', '最新测试电子邮件', '测试电子邮件正文'))打印邮件发送成功";别的打印发生错误";

错误期间的日志文件读取如下:

  • /var/log/httpd/error_log :msmtp:无法连接到 smtp.gmail.com,端口 587:权限被拒绝msmtp: 无法发送邮件(来自/etc/msmtprc 的默认帐户)

/etc/msmtprc 包含:

默认值授权开启tls_trust_file/etc/pki/tls/cert.pem账户默认主机 smtp.gmail.com587端口用户 emily@emilytench.net来自 emily@emilytench.net密码 [******]授权系统登录

欢迎任何指向正确方向的指针......只是试图为localhost php邮件功能通过我的gmail smtp服务器发送电子邮件实现一个简单的途径 - 这不是生产服务器配置,它是我的本地apache/php用于网页开发的网络服务器

解决方案

抱歉回复晚了.我自己也在这个问题上挣扎.问题是配置文件的文件权限.

如果您没记错的话,我们会要求您将文件 chmod 改成 0600,否则它就无法工作.并且您可能使用与您的网络服务器/php 不同的用户创建了该文件.

这意味着您的网络服务器或控制 PHP 的服务器无法读取该文件来获取您的电子邮件配置.

此外,如果您在 ~/.msmtprc 下创建了配置文件,那也将不起作用.因为当与 PHP 一起使用时,MSMTP 只使用 /etc/msmtprc

中的全局一个

这意味着您必须在 /etc/msmtprc 中创建配置,然后 chown 配置文件以匹配您的 webs-erver/php 用户.

由于我在 Debian 上使用 NGINX,因此我必须使用 chown www-data:www-data/etc/msmtprc 使 www-data 可以访问该文件在 CentOS 上,该用户可能是 httpd 所以请确保您正确设置了该用户.

这样做之后,我可以使用 PHP 使用 MSMTP 发送邮件,没有任何问题.

linux (fedora 20) msmtp configuration sends from shell but fails from PHP/apache, I am stumped... my objective is just to send email, through my gmail smtp from my localhost development webserver, to test output of code that sends mail

php.ini sendmail file reads : sendmail_path = /usr/bin/msmtp --debug -C /etc/msmtprc --read-recipients

there is only one php.ini on the system, used for both CLI and webserver located at /etc/php.ini

permissions on /etc/msmtprc are set to apache:apache 600

the following commands as root work and produce a test email :

  • php -r "mail('emily@emilytench.net', 'Newest Test Email', 'Test email body');"
  • runuser -l apache -c '/usr/bin/msmtp --debug -C /etc/msmtprc --read-recipients < /var/www/html/test.mail' (test.mail includes to and from lines)

but apache/php produces an error when the php mail function is called from the following script:

if (mail('emily@emilytench.net', 'Newest Test Email', 'Test email body'))
print "Email successfully sent";
else
print "An error occured";

Log files during error read as follows :

  • /var/log/httpd/error_log : msmtp: cannot connect to smtp.gmail.com, port 587: Permission denied msmtp: could not send mail (account default from /etc/msmtprc)

/etc/msmtprc contains :

defaults
auth on
tls on
tls_trust_file /etc/pki/tls/cert.pem
account default
host smtp.gmail.com
port 587
user emily@emilytench.net
from emily@emilytench.net
password [******]
auth on
syslog on

any pointers in the correct direction are welcomed... only trying to achieve a simple avenue for localhost php mail function to send emails through my gmail smtp server - this is not a production server configuration, it is my local apache/php webserver for web development

解决方案

Sorry for the late reply. I also struggled with this issue my self. The problem was the file permissions on the configuration file.

If you remember correctly you we're asked to chmod the file to 0600 because it wouldn't work otherwise. And you probably created that file using a different user than the one of your web-server/php.

Which means that your web-server or the one controlling PHP cannot read that file to get your email configurations.

Also if you created your configuration file under ~/.msmtprc that also won't work. Because when used with PHP, MSMTP only uses the global one from /etc/msmtprc

Which means that you must create your config in /etc/msmtprc and then chown the configuration file to match the user of your webs-erver/php.

Since I was on Debian and I used NGINX I had to make that file accessible to www-data with chown www-data:www-data /etc/msmtprc On CentOS that user might be httpd So make sure you have that user set correctly.

After doing that I was able to send mails with MSMTP using PHP with no problems.

这篇关于linux msmtp 配置从 shell 发送但从 PHP/apache 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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