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 -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!

  1. #1 by Derek Organ at December 12th, 2007

    Nice on on the the guide.. so much handier than messing around with sendmail and postfix installs.

  2. #2 by Luciano at July 11th, 2008

    This just saved me a lot of time, thanks!

  3. #3 by Oliver at July 21st, 2008

    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.

  4. #4 by David Hurst at July 21st, 2008

    Oliver,

    Glad I could be of help. Feel free to print and save.

    Regards,

    David

  5. #5 by juan at November 11th, 2008

    Thanks your solution worked perfectly i have been working on this issue for 5 hours thanks

  6. #6 by A-NOM-ynous at November 11th, 2008

    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.

  7. #7 by David Hurst at November 13th, 2008

    Cheers A-NOM. That’s a better way of doing things - I’ve updated the article above.

  8. #8 by DaWaBZ at January 10th, 2009

    This is just a thank you for the Hint
    I was searching for such a simple - yet efficient - solution
    Thanks a lot !

  9. #9 by THE_PATRICIAN at April 14th, 2009

    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

  10. #10 by ap at April 29th, 2009

    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!

  11. #11 by ct at June 8th, 2009

    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

  12. #12 by nobrell at July 6th, 2009

    Thanks David for a gr8 guide :D
    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()

  13. #13 by Eduardo López at September 1st, 2009

    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

  14. #14 by Anonyme at September 4th, 2009

    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?

  15. #15 by David Hurst at September 4th, 2009

    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.

(will not be published)
  1. No trackbacks yet.