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 -tPHP 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.
This is just a thank you for the Hint
I was searching for such a simple – yet efficient – solution
Thanks a lot !
Wow, I messed around with postfix and sendmail for hours, this took a few mins, have my babies
Just a tip on ports, I found that you can use this, changing you smtp and port to suit
mailhub=smtp.Yourdomain.com:80
Wow. I just spend two days getting my php scripts to send e-mail. Tried setting up postfix, nullmailer, esmtp and ssmtp. All of them worked fine from the command line, but my apache2 error.log kept saying that sendmail couldn’t be found. I’m just learning to administer a Linux server though ssh (which is extremely satisfying once you get something to work) and blog posts like this are a great help for guys like me. Learning as i go.
So, just wanted to say: thanks a bunch, keep ‘m coming!
thanks for the guide!
tried to get ssmtp to work on gentoo for 1 day.
didn’t see the authmethod directive .. omg
with this directive, all is working great
Thanks David for a gr8 guide
Works like a charm, and I don’t need to install and configure sendmail on my LAMP just to be able to send out PHP mail()
Greetings and thanks for the guide.
I just wanted to point out that, in my personal case, i had troubles making ssmtp work because of the structure of my user’s password, which contained a special character “#”. I didnt make it work until I realized that, changed the password and all worked perfeclty
I hope that would help someone!
Regards
mailhub= line to the address of your SMTP server.
What is that? I want to DO smtp server, and that i need always HAVE SMTP server?
If you read the first line of the post you will see that you need to have a working SMTP server to use SSMTP. SSMTP is not a SMTP server – it’s just a utility that forwards mail to a designated server and behaves like sendmail on the command line. This is perfect for when you have a PHP server that you do not want to run a full MTA on.
If you need a full MTA, you will need to install sendmail, postfix or something similar.
I found an error when i test a php code,
ssmtp: 553 You are not authorized to send mail, authentication is required
but finally I fixed, in the mail function, which needs the ‘From’ header, so it should be mail($to, $title, $body, ‘From: aaa@stmp.com‘);
I posted here so that it would be helpful for others.
Robbin,
This will very much depend on the configuration of the SMTP server you are using and how it is set to authorise. Thanks for posting though, might help someone else with the same issue.
Regards,
David
You’ve saved me so much time with this! Here is how I used it: http://serverfault.com/q/225308/59562 . Thank you thank you thank you!!!
David, thank you very much for the information.
I use Lighttpd Server instead of Apache Server. Can you tell me what is the equivalence of “/etc/php5/apache2/php.ini” ?.
Great tutorial David.
Thanks
You can also send mail from gmail account by using smtp.gmail.com:587 server http://ubuntu-for-humans.blogspot.com/2010/04/sending-emails-from-ubuntu-without.html
Hello David, i did all the suggested changes on my Debian Squeeze server. But i can’t send e-mail.
Should i also leave the settings SMTP localhost
and smtp_port 25 in php.ini ?
also when i do
ps ax | grep ssmtp i do not get any running processes.