can't use mail() on nginx but can send mail on console using msmtp

The Wolf asked:

I setup msmtp on my centos 6, i know its working fine because I can send email using bash command:

echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=default -t [email protected]

then, I am moving on to allowing nginx to also allow the use of msmtp, in my php.ini this is my sendmail path:

sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc --logfile /var/log/msmtp.log -a gmail -t"

then, I tried sending using this:

if(mail("[email protected]","A Subject Here","Hi there,\nThis email was sent using PHP's mail function."))
print "Email successfully sent";
print "An error occured";

But, it keeps failing, I am starting to think it is because of the permission because nothing was written in the logs, here are the permissions I have

for msmtprc @ /etc/

[[email protected] ~]# cd /etc/
[[email protected] etc]# ls -l
-rw-------  1 nginx   nginx     248 Apr  2 14:55 msmtprc

for /var/log

-rw-r--r-- 1 nginx  nginx    788 Apr  2 14:56 msmtp.log

Is it permission issue? or other else. to whom should I chown is it for nginx? or other else?

Hope somebody can help, I’d been doing trial and error for hours and it has been frustrating. Thanks!

My answer:

Since CentOS 6.6, SELinux policies that applied to Apache are now also applied to nginx and php-fpm in the same way. Thus you need to use the right SELinux boolean to allow the web server to send mail.

setsebool -P httpd_can_sendmail 1

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.