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
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.
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
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.
FIXED - msmtp: cannot log to /var/log/msmtp: cannot open: Permission denied
This is for the next person who runs into this issue.
System config file for msmtp -rw-rw-rw- 1 root root 266 Jun 3 06:07 /etc/msmtprc
# mimecast
account mimecast
host smtp.mail.com
port 587
protocol smtp
from admin@company.com
auth on
user authuser@company.com
password mypassword
tls on
tls_certcheck off
logfile ~/.msmtp.log
syslog off
account default : mimecast
.#mimecast is just a section header and can be deleted
account mimecast - is a title if multiple send accounts are available or needed
account default : mimecast - is saying this is the default account used
The configuration file per user if needed can be the same as the system file with a different userid, password and from fields. note the "." before the .msmtprc
-rw------- 1 ubuntu ubuntu 267 Jun 3 05:50 .msmtprc
The log file gets created per user in their home directory with the correct permissions - no need to mess with the permissions.
-rw-r--r-- 1 root msmtp 344 Jun 3 06:09 .msmtp.log
To send an email from the command line
echo -e "Subject: MySubject\r\n\r\nThis is mybody" |msmtp recipient@company.com
use the -C configfilename to specify alternate local config files
use the -a account mimecast to switch between accounts to send from within the config file ( did not try this option )
or use
msmtp recipient@company.com
Subject: This is my subjectline
Blank line ( press enter )
Here is the body of the email
CTRL-D ( to send )
or use this option to send mail from the command line
msmtp recipient@company.com < filename
where the filename contains
To: recipient@company.com
From: sender@company.com
Subject: Here is the Subject
body body body .....
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 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.