PHP mail() and ssmtp on Debian Linux
If, like me, you have a dedicated mail server that you want your PHP scripts to use, you may have come across ssmtp. ssmtp is a cut-down MTA that simply sends mail off to another server - ideal for scenarios where you don’t need to receive mail on your web server, but you do want to be able to use the PHP mail() function.
It’s very simple to set up, and it is not, as some have said, impossible to get PHP to use ssmtp. These instructions are for Debian. Should also work with Ubuntu.
First of all, install ssmtp with apt-get install ssmtp
Next you need to edit the conf file at /etc/ssmtp/ssmtp.conf. Set the mailhub= line to the address of your SMTP server. Put your hostname on the hostname= line, and put FromLineOverride=YES.
Now, if your SMTP server requires authentication, you will need the AuthUser, AuthPass and possibly AuthMethod flags below - add these to the bottom of the ssmtp.conf:
AuthUser=your smtp username here
AuthPass=your smtp password here
AuthMethod=LOGIN
Now you need to edit your php.ini. On Debian, you will likely find this in /etc/php5/apache2/php.ini - obviously change the directories for the appropriate versions of PHP and Apache on your system. (Quick tip: not sure where a file might be? Type updatedb at the command line, and when that’s finished, locate filename). In your php.ini, find the sendmail_path= line.
sendmail_path = /usr/sbin/ssmtp -t
PHP requires the -t flag.
Save your php.ini and restart Apache (probably /etc/init.d/apache2 restart). Your mail() function should now work.
Enjoy.
Nice on on the the guide.. so much handier than messing around with sendmail and postfix installs.
This just saved me a lot of time, thanks!
Hey David,
Awesome tutorial, saved a load of time, and ssmtp works very fast and its really recommend. Was using sendmail and was really slowwwww, like 2mins or a timeout before sending a few KB of mail.
Nice, and thanks, I’ll print this for personal reference if you dont mind.
Oliver,
Glad I could be of help. Feel free to print and save.
Regards,
David
Thanks your solution worked perfectly i have been working on this issue for 5 hours thanks
A slightly more “secure” method that doesn’t expose your SMTP username and password to anyone who sees a phpinfo() page is to set sendmail_path = “/usr/local/sbin/ssmtp -t” and use AuthUser and AuthPass in ssmtp.conf.
Also, ssmtp doesn’t have a -i flag, so it just gets ignored.
Cheers A-NOM. That’s a better way of doing things - I’ve updated the article above.