Matt Browne
Matt Browne

Reputation: 12419

Is it possible to configure sendmail to use SMTP authentication by default (for use with PHP)?

I am maintaining an old PHP application and I would like to configure it to use Mandrill for outgoing email. Mandrill, of course, requires SMTP authentication. The application uses PHP's mail() function directly.

Is there any way I can configure sendmail (or an equivalent service) to send using SMTP authentication by default (with the credentials for Mandrill), without having to replace all the mail() calls throughout the application?

I saw some other answers about SMTP configuration in php.ini but that only works on Windows, so I believe the only PHP setting I could change that would potentially be useful is sendmail_path, which defaults to sendmail -t -i. I was thinking maybe I could point it to a shell script instead but I'm not sure how to go about that or whether it would work.

UPDATE

Thanks to @mti2935's answer, I was able to get this working. I had trouble getting sSMTP to work (got the error "send-mail: Cannot open smtp.mandrillapp.com:587" even though there was no firewall blocking it) so I followed the second link and set up MSMTP. I had to change the tls_trust_file setting to /etc/pki/tls/certs/ca-bundle.crt (note: I'm running CentOS 6). Also, the sendmail_path PHP setting 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 since I changed this in php.ini rather than in an .htaccess file...note that the config file can be called whatever you want; choose your own name in place of "myconfig").

Also, when testing, be sure to specify a "From" address, otherwise some destinations including Gmail might reject the message.

Upvotes: 3

Views: 6561

Answers (2)

mti2935
mti2935

Reputation: 12017

There are a number of lightweight replacements for sendmail that can be used to relay outgoing messages through a remote SMTP relaying server, such as SSMTP, MSMTP, and Nullmailer. By replacing /usr/sbin/sendmail with one of these, you can relay outgoing mail sent from your PHP scripts through a remote SMTP server, without making any changes to your PHP scripts that use the PHP mail() function. These replacements simply handoff the message to the relaying server - the don't handle incoming mail, they don't manage a queue, etc.

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 may be to continue using sendmail, configured with a smarthost. See https://serverfault.com/questions/41448/fastest-way-to-allow-sendmail-relay-through-smarthost

Upvotes: 2

Greg K
Greg K

Reputation: 434

Swiftmailer is a good option. You should also take a look at Zend2 Mail. I used this on a small project and it fit the bill nicely.

PHP Storm has a pretty cool search and replace function. If you just search for let's say "mail(" (without the quotes) you can manually go through the results and confirm before replacing them. Or you can do a replace but before it replaces each occurrence, you can confirm it.

Upvotes: 1

Related Questions