Linux – PHP mail() not working with Postfix on ubuntu 16.04

linodelinuxPHPUbuntu

OK, I realize this question has been asked, but the issues relating to my specific issues weren't brought up.

Basically, I barely know what I'm doing in the server, but I need to configure it to send out emails and not have them end up in the spam folder. Currently the major issue I seem is this error message in my mail.log file:

error: to submit mail, use the Postfix sendmail command

fatal: the postfix command is reserved for the superuser

In my php.ini file I have sendmail_path = /usr/sbin/sendmail -t -i

Postfix is configured with my hostname, when I send mail from the command line it sends it out just fine, so everything is working there, but when in the CLI I am logged in as root, so maybe it's not an issue because I have access. When I run postfix status I get the following:

postfix: Postfix is running with backwards-compatible default settings
postfix: See http://www.postfix.org/COMPATIBILITY_README.html for details
postfix: To disable backwards compatibility use "postconf compatibility_level=2" and "postfix reload"
postfix/postfix-script: the Postfix mail system is running: PID: 13198

I am down to the wire on this project and really need to resolve this quickly but all the zillion different tutorials seem to really differ on how to set this all up correctly, or at least I'm not understanding what I did wrong.

To rehash, the main goal is to get PHP to actually send them email when I call

mail($to, $subject, $msg, $headers);

Best Answer

Thank you @bgtvfr those were all working when I tested it. Sending mail from the command line was working just fine as intended.

The errors I listed from my mail.log were caused by my php.ini file not being configured correctly.

At one point I set sendmail_path = /usr/sbin/posfix and then while I had changed it back to the above code, pointing to sendmail I had not reloaded the php.ini settings. I'm running ubuntu with nginx and php7.0, however restarting nginx will not reload your php settings, so I had to run the following:

sudo /etc/php7.0/fpm/php.ini restart

Once this was reset to sendmail_path = /usr/sbin/sendmail then it started working as intended.