A
A
altai20132017-12-18 11:29:58
PHP
altai2013, 2017-12-18 11:29:58

Sending mail via msmtp - why does it work from the user's console, but not from the web server?

I installed msmtp on the server (ubuntu 16.04) to send mail via Yandex smtp and I can't debug the disappearance of messages without a trace, which occurs when trying to use mail() in the php files of the web server. In the console everything works fine, I do

php -r "mail('[email protected]', 'Test Subject', 'Test Message');"
and the letter instantly arrives at [email protected]
I make the same call to mail () from a php-file through a browser - nothing happens.
The standard msmtp log (/var/log/msmtp/msmtp.log) records only sending mail from the console. Calls from the web server are not reflected in it in any way. There are no errors in the nginx log (/var/log/nginx/error.log). In php.ini (/etc/php/7.0/fpm/php.ini) I wrote:
sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a myaccountname -t"
mail.log = /var/log/maillog

Mail() calls from the server get into the maillog log, but they look somehow strange:
[18-Dec-2017 15:09:30 Asia/Krasnoyarsk] mail() on [/var/www/mydomain.com/html/wp-content/themes/mywebsite/send.php:23]: To: [email protected] -- Headers:

That is, there is practically no useful information and I do not see any errors. :(
Maybe the problem is in the msmtp configuration file? When calling mail() from the user's console, the config is read from the user's home directory and this script works fine. When calling mail() from a file on the web server, msmtp is started as the www-data user, in which case it should read the configuration file in the default directory: /etc/msmtprc. This file exists, its content is the same as the file in the user's home folder ~/.msmtprc The permissions to access the file are the same (0600) in both /etc/msmtprc and ~/.msmtprc, only in the first case the owner file is www-data (so that nginx can read it), and in the second - a normal user whose home folder contains the config. I do not see an error in the configuration, but it is obvious that there is one if the mail does not leave the web server. Tell me where else you can dig and where to look,

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
altai2013, 2017-12-19
@altai2013

Cause of the problem: when sending mail from the console, PHP uses one config (/etc/php/7.0/cli/php.ini), and when calling mail() from the php files of the web server, another one (/etc/php/7.0 /fpm/php.ini). In one of the configs, the correct value of the sendmail_path variable (it is also default): and in the other, it is incorrect: sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a default -t" For this reason, mail was sent from the console, and from php files was not sent. The solution to the problem is to have /etc/php/7.0/fpm/php.ini have the default value for the sendmail_path variable, even after setting msmtp.

V
vinoff, 2017-12-18
@vinoff

Perhaps it will be easier to debug this way
sudo -u www-data php -r "mail('[email protected]', 'Test Subject', 'Test Message');"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question