4

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

Emily
  • 61
  • 1
  • 2
  • 5

6 Answers6

4

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.

SLC
  • 2,167
  • 2
  • 28
  • 46
  • Just a note: I'm using the msmtp package (from the EPEL depo) on CentOS 6 and I was able to use ``/etc/msmtprc`` with the chmod set to ``0644`` and chown set to ``root:root`` no problem. I'm not sure where the requirement to set chmod to ``0600`` and chown to the user that would be using msmtp came from but it's not required. – SameOldNick Apr 02 '15 at 19:25
  • the user the php process run with and execute msmtp should be at least able to read the *msmtprc* file, also you're not forced to use `/etc/msmtprc` just use `-C` to specify a path, just make sure the same user above can go through the parent directories – Alex Jun 11 '17 at 15:47
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

tylersDisplayName
  • 1,603
  • 4
  • 24
  • 42
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');"
ikxx
  • 61
  • 3
  • 7
    The documention of msmtp doesn't really tell you, but invoking -C is what triggers the ownership check – not the fact that it's /etc/msmtprc (the system default file. If you omit the -C /etc/msmtprc part completely, it will default to /etc/msmtprc and skip the ownership check. – Fuzzy76 Jun 19 '17 at 12:16
  • @Fuzzy76 - Beautiful. Thank you so much! Fixed the problem for me. – rinogo Sep 06 '18 at 23:13
2

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.

joec4i
  • 31
  • 2
1

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.

Emily
  • 61
  • 1
  • 2
  • 5
1

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 .....
2anoyu
  • 11
  • 2