PHP – Can I configure sendmail to use SMTP authentication by default (for PHP)?

Can I configure sendmail to use SMTP authentication by default (for PHP)?… here is a solution to the problem.

Can I configure sendmail to use SMTP authentication by default (for PHP)?

I’m maintaining an old PHP application and I want to configure it to use Mandrill for outgoing email. Of course, Mandrill requires SMTP authentication. The application uses PHP’s mail() function directly.

Is there any way to configure sendmail (or equivalent) to send with SMTP authentication by default (using Mandrill’s credentials) without having to replace all mail() calling applications throughout the process?

I’ve seen some other answers in php .ini about SMTP configuration, but this only works for Windows, so I’m sure the only PHP setting I can change that might be useful is sendmail_path, which defaults to sending mail -t -i. I was thinking maybe I could point it to a shell script, but I’m not sure how to do it or if it would work.

Update

Thanks to @mti2935’s answer, I was able to get it to work. I had trouble getting sSMTP to work (with the error “Send mail: Unable to open smtp.mandrillapp.com:587” even though there is no firewall blocking it) so I clicked on the second link and set up MSMTP. I had to change the tls_trust_file settings to /etc/pki/tls/certs/ca-bundle.crt (note: I’m running CentOS 6). Also, the sendmail_path PHP settings recommended in the link didn’t work for me; I had to change it to “/usr/bin/msmtp -C/etc/msmtp/myconfig -t” (and restart Apache because I changed it in php.ini instead of .htaccess file… Note that the configuration can call the file at will; Choose your own name instead of “myconfig”).

Also, be sure to specify the “From” address when testing, or some destinations, including Gmail, may reject the message.

Solution

There are many lightweight alternatives to sendmail that can be used to relay outgoing messages through remote SMTP relay servers, such as SSMTP, MSMTP, and Nullmailer. By replacing /usr/sbin/sendmail with one of these, you can relay outgoing mail sent from PHP scripts through a remote SMTP server without making any changes to PHP scripts that use the PHP mail() function. These substitutions simply pass messages to the relay server – they don’t process incoming messages, they don’t manage queues, and so on.

See:
http://itekblog.com/ssmtp-a-gmail-sendmail-relay-replacement-for-linux/
http://www.emanueletessore.com/how-to-configure-msmtp-as-a-gmail-relay-on-ubuntu-server/
http://untroubled.org/nullmailer/

Another option might be to continue using SendMail configured with a smart host. See https://serverfault.com/questions/41448/fastest-way-to-allow-sendmail-relay-through-smarthost

Related Problems and Solutions