1

My problem is that php script which has to send mail doesn't send it if I run it in browser, but works in cli. This doesn't help PHP's mail() function working in CLI but not in Apache

In both /etc/php/7.0/fpm and /etc/php/7.0/cli same mail settings:

SMTP = localhost
smtp_port = 25 
sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -t"
mail.add_x_header = On
mail.log = /var/log/maillog

If I type in terminal

php -r mail("my_mail@gmail.com", "Test", "test");

it works, also it works if I type

sudo -u www-data php -r mail("my_mail@gmail.com", "Test", "test");

However, if I try to send mail from php interactive mode php -a and then simply type there mail("my_mail@gmail.com", "Test", "test"); I get /etc/msmtprc permission denied. I tried to chmod 666 /etc/msmtprc (however ls -al shows -rw------- 1 www-data www-data 267 Apr 4 11:10 /etc/msmtprc, so I think no need in allowing access) I get error like msmtp: /etc/msmtprc: contains secrets and therefore must be owned by you file msmtprc is chmodded this way chmod 600 msmtprc, according to many tutorials.

Pavlo Kovalov
  • 21
  • 1
  • 3

1 Answers1

1

Somehow I understood what is wrong simply attempting different configurations, so writing answer to my own question :)

in php7, there are apache2, fpm, cli, and mods-available folders in /etc/php/7.0/ Some of the tutorials/questions say that to make msmtp work you have to edit php.ini in fpm and cli folder. That's not fully true. To make apache2 working with msmtp so you could use mail() function to send emails in php scripts, you have to configure php.ini in /etc/php/7.0/apache2. The mail configuration of all 3 php.ini files should be the same, which is

SMTP = localhost
smtp_port = 25 
sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -t"
mail.add_x_header = On
mail.log = /var/log/maillog

Note, that -C option shows the path to your msmtprc file, which should be the same as here. Also note that since you have installed and configured msmtp, so you can send mails from command line (not using php), you DON'T need to install sendmail on your server. Simply configure you php.ini files and everything will work

Pavlo Kovalov
  • 21
  • 1
  • 3