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

纵然是瞬间 提交于 2019-12-10 10:30:00

问题


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


回答1:


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.




回答2:


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');"



回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/25880689/linux-msmtp-configuration-sends-from-shell-but-fails-from-php-apache

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