Enabling PHP mail() in Debian easily and quickly

13
Printer-friendly versionSend to friendPDF version
No votes yet

I've run into a problem more than once where the mail() function in PHP didn't work. Unlike with a Windows deployment, you can't simply set an SMTP server in the php.ini file; you must set a value for the sendmail_path.

If you're running a mail server like Sendmail or Postfix alongside your web server, this is no big deal. But for the rest of us, do we really need a big beefy MTA just to mail-enable PHP?

No, we don't! I found a very simple way to get PHP sending mail without setting up a full-blown MTA, using sSMTP. sSMTP is not a running daemon, does not have a queue, and it cannot accept mail for delivery -- all it does is relay a message immediately and directly to the SMTP server it's configured to use. For most PHP users, this is a perfect solution.

  1. First, install ssmtp
    apt-get install ssmtp
  2. Next, edit the configuration file at /etc/ssmtp/ssmtp.conf.
    1. Set the mailhub= line to the name or address of your SMTP server
    2. Set the hostname= line to the actual host name of this server
    3. Set FromLineOverride=YES, otherwise, the From: set in any PHP application will be ignored
    4. If your SMTP server requires authentication, then
      • Set AuthUser= to the SMTP username
      • Set AuthPass= to the correspnding password
      • Set AuthMethod=LOGIN
  3. Now you need to configure PHP. In Debian, it's recommended to create new .ini files inside of /etc/php5/conf.d instead of editing the php.ini file directly. Create a mail.ini file inside /etc/php5/conf.d, and add the following line:
      sendmail_path = /usr/sbin/ssmtp -t
    

    PHP requires the -t option, and the usual -i option is ignored

  4. Save the mail.ini and restart Apache
    /etc/init.d/apache2 restart

Now, the mail() function should work as designed!

Post new comment

The content of this field is kept private and will not be shown publicly.
 
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <hr> <span>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options