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: https://forum.bytemark.co.uk/comments.php?DiscussionID=2701

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: 127.0.0.1
  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(‘myself@example.com’,’hello’,’my message reached you’); this should send the message correctly to the myself@example.com 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.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone

4 Comments

  1. I am having one minor problem… I messed with some configuration in the php.ini file with the mail function trying to get sendmail to work (never could get it to work, though) and I think something might have gone wrong. What should be in my php mail function? What should/shouldn’t be commented out? (It didn’t work, by the way)

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *