Skip to content

Sending mail from PHP on Gentoo

by Ole Markus on August 7th, 2010

Having been around in #gentoo-php on freenode, there is especially one support question that needs to be frequently answered there: the mail() function in PHP doesn’t work, returning the error sendmail: Cannot open mail:25.

This does not mean that you need to install sendmail or a full mail server such as postfix.

Also, it does NOT mean that PHP is configured incorrectly. The error does not originate from PHP. Do not mess with php.ini

How mail is sent

In order to understand why this happens, you need to understand how mail is being sent. In Linux/UNIX land, mail is very often received through a sendmail interface belonging to a mail transfer agent (MTA) that sends it through other MTAs using the SMTP protocol until the mail reaches the final MTA that drops the mail in your inbox.

How PHP sends mail

A default installation of php depends on virtual/mta. This virtual simply means “give me an MTA, but I don’t care which”. The default MTA in Gentoo is ssmtp, which is a lightweight MTA that basically provides a sendmail interface and relays all mail received through this interface to a fixed SMTP server somewhere else. Ssmtp is not able to figure out where the mail is supposed to end up. It can only forward mail to another MTA that has this ability.The advantage of using ssmtp or other lightweight sendmail implementations is that clients such as a PHP-enabled web server does not have to run a powerful SMTP server such as postfix or qmail which has far too many features you don’t need and thus consumes much more resources than necessary.

Finding your SMTP server

As stated, in order to use ssmtp you need something like postfix running somewhere else. Luckily, you Internet Service Provider (ISP) or hosting provider probably already have one that you can use. Check with your ISP if they offer an SMTP server that you can send mail through, often looking something like smtp.serviceprovider.org or mail.isp.net.

Configuring ssmtp

Once you have found an SMTP server you can use, lets call it mail.isp.net, you need to configure ssmtp to relay mail to this server.

  1. Open /etc/ssmtp/ssmtp.conf.
  2. Go to line 7, which should read mailhub=mail
  3. Change this line to read mailhub=mail.isp.net
  4. This is not strictly necessary, but you probably want to go to line 34 and make sure it reads FromLineOverride=YES. This ensures that you can change the origin address (From header) of the mails that you send.

Using ssmtp to relay mail through gmail

Now, as an added bonus, if you do not have access to an SMTP server, but you have a gmail (or googlemail) account, you can configure ssmtp to send mail through your gmail account.

Just change your /etc/ssmtp/ssmtp.conf to something like this:

mailhub=smtp.gmail.com:587
AuthUser=<your gmail user>@gmail.com
AuthPass=<your gmail password>
UseSTARTTLS=YES

From → Gentoo Linux, PHP

One Comment
  1. Franz permalink

    Very useful indeed!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

Switch to our mobile site