Making PHP mail() work with Sendmail/Postfix

Posted under » PHP » Ubuntu on 20 April 2017

Sending mail from your server can be done via an internal SMTP or an external one.

For the latter, you can use PHPMailer.

But sometimes, our own server on localhost is preferred.

Do do this, you need to have either Sendmail or Postfix.

You cannot have both MTA at the same time so I am more interested in the one that I use... ie. Postfix

All you need to do is config this on your php config file or PHP.ini.

sendmail_path = "/usr/sbin/sendmail -t -i"
// in case you want to see the logs from the php point of view
mail.log = /var/log/phpmail.log

For backward compatibility purposes, usually /usr/sbin/sendmail is used. Sendmail was around first and pretty much everything just assumes it is in the installed MTA. So when it isn't, the replacement makes a symlink so that nothing breaks. So it doesn't matter if you use Postfix or Sendmail.. it will still work.

If it doesn't work, you can try this.

sendmail_path = /etc/postfix

FYI, The first option worked for me.

Send an email test to see if it works.

        ini_set( 'display_errors', 1 );
        error_reporting( E_ALL );
        $from = "";
        $to = "";
        $subject = "PHP Mail Test script";
        $message = "This is a test to check the PHP Mail functionality";
        $headers = "From:" . $from;
        mail($to,$subject,$message, $headers);
        echo "Test email sent";

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS