Howto send mails with PHP + Ubuntu

Update 2010-12-21: A really serious security flaw has been detected in Exim 4 < 0.70. Please make sure your version is secure:

If you’re a developer who works with Ubuntu or Debian, or at least with PHP and Apache, you will have the problem at some point that you want to send e-mails from your web application (for example using the PHP mail() function).

The most logical for a PHP developer is to use the default configuration of PHP: sendmail. However, sendmail has a enough enemies and Wikipedia has this to say about sendmail

Sendmail can be difficult to configure. Whereas nearly all popular Unix based server software has human readable configuration files, Sendmail’s configuration is not considered human readable. Instead, the Sendmail authors recommend that administrators learn and use macro language tools, particularly M4 to configure Sendmail. Sendmail is unique amongst Unix based MTAs in this requirement, and no MTA developed since Sendmail requires or recommends the use of macro languages for configuration.

What is unclear when you install sendmail is that it is, in fact, a very poorly usable system, which requires a lot of configuration to work well, and this configuration is made very complicated, as Wikipedia explains.

One solution is then to use Exim4, which can be quickly configured (but not *that* easily).

To install Exim4, we first need to remove sendmail if it is present

sudo apt-get remove sendmail

The apt-get indicates that we can remove a series of packages initially installed with sendmail, using the following command

sudo apt-get autoremove

Now we get to install exim

sudo apt-get install exim4

This installs a series of packages, but it’s not enough, you also need to configure exim:

sudo dpkg-reconfigure exim4-config

The system then opens a colorful terminal which can  be browsed this way:

  1. OK
  2. Internet distribution (SMTP)
  3. Name of the system e-mail: the name of the computer (or any name, just avoid funny characters)
  4. OK
  5. IP addresses:
  6. Other destinations: localhost
  7. Relays: leave empty
  8. Relays 2: leave empty
  9. OK
  10. Minimize DNS requests DNS: yes (it is a development machine, not a server)
  11. Maildir Format (we’re developers, we want things made easy)
  12. Split the configuration: No (there’s not much in there anyway)
  13. Done

At the end of the process and of restarting Apache (sudo /etc/init.d/apache2 restart), use a PHP script which contains, for example: mail(‘’,’hello’,’my message reached you’); this should send the message correctly to the address.

Finally, if you ever end up with a big queue (mailq or mailq -d command) of e-mails that are **frozen** because your previous configuration was wrong, you can send them anew with exim -qff.

Related Posts

Howto debug templates in Akelos

One of the problems with having everything built into a framework is that,...

Measuring memory usage with XDebug no longer possible (as of 2.0.0 RC4)

For those like me who remember being able to see memory usage in...


Comments are closed.