linux msmtp configuration sends from shell but fails from PHP/apache

隐身守侯 提交于 2019-12-06 04:41:41

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.

I had the symilar error msmtp: /etc/msmtprc: must be owned by you with openSuse and changing the owner of /etc/msmtprc was not an option since cron and other services use it for other purposes and it resulted with another error msmtp: /etc/msmtprc: must have no more than user read/write permissions

My solution was to:

1) as root create a copy of msmtprc

cp /etc/msmtprc /etc/msmtprc_apache
chown wwwrun:www /etc/msmtprc_apache
chmod 0600 /etc/msmtprc_apache

2) change apache php.ini settings (search for sendmail_path) and force the configuration file (-C option)

sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc_apache -t"

3) comment out in apache php.ini settings

; SMTP = localhost
; smtp_port = 25

For simple testing, as root switch to wwwrun user and test with php

sudo -u wwwrun -s
php -r "mail('test@test.com', 'PHP test', 'Test from PHP as wwwrun user');"

I had the problem of MSMTP sending from shell but not working via PHP on CentOS 7. After spending the entire day on this my solution was to...

sudo -u {apacheUser} -s which msmtp

For me, this outputted /bin/msmtp not user/bin or any local bins. Once I updated my sendmail_path in PHP.ini with the path used by the Apache user everything worked perfectly.

Final solution, for me:

sendmail_path = /bin/msmtp -t -i

Also, maybe it should be noted that I have commented SMTP and smtp_port in my php.ini

I couldn't change the file owner due to mstmprc being mounted from a kubernetes secret. Replacing password with passwordeval did the trick.

passwordeval "echo the-password"

It's obviously not the most secure way so ideally echo should be replaced with an encryption tool.

I'm seeing this question asked, unanswered, in a number of forums - and even ran into my own question in a site that "scrapes" content from stack overflow - and posting an answer to this question for anyone confused by this issue. While this is not an exact answer to the question, it has something to do with the gnome key-ring support that was added to msmtp, as it is run without a shell and with tls. Unable and unwilling to try and convince the code to act in a way in which it was not designed to, my solution has been, with some resistance, to configure exim for smtp relay instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!