Emily
Emily

Reputation: 61

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

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 :

but apache/php produces an error when the php mail function is called from the following script:

if (mail('[email protected]', 'Newest Test Email', 'Test email body'))
print "Email successfully sent";
else
print "An error occured";

Log files during error read as follows :

/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 [email protected]
from [email protected]
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

Upvotes: 4

Views: 16639

Answers (6)

2anoyu
2anoyu

Reputation: 9

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 [email protected]
auth on
user [email protected]
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 [email protected]

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 [email protected]
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 [email protected] < filename

where the filename contains

To: [email protected]
From: [email protected]
Subject: Here is the Subject

body body body .....

Upvotes: 1

joec4i
joec4i

Reputation: 41

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.

Upvotes: 3

tylersDisplayName
tylersDisplayName

Reputation: 1643

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

Upvotes: 3

ikxx
ikxx

Reputation: 61

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('[email protected]', 'PHP test', 'Test from PHP as wwwrun user');"

Upvotes: 2

SLC
SLC

Reputation: 2207

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.

Upvotes: 4

Emily
Emily

Reputation: 61

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.

Upvotes: 1

Related Questions