Home > Documentation, English, php, Tech Crunch > Howto send mails with PHP + Ubuntu

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:
  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.

Categories: Documentation, English, php, Tech Crunch Tags: ,
  1. August 7, 2010 at 3:56 am

    $ sudo apt-get install sendmail

    Restart apache and forget about it ;)

    • ywarnier
      August 7, 2010 at 4:32 am

      I don’t remember the specifics clearly (it’s been some time since we wrote this article in Spanish the first time), but there are many cases where it simply doesn’t work that easily with sendmail.

    • February 3, 2011 at 5:11 pm

      This post was very useful for me for many times, but
      this post doesn’t work anymore, I don’t know why…
      I used exim4 with gmail and work perfectly, I’m sharing this link : http://www.esdebian.org/articulos/24027/como-enviar-correos-ips-dinamicas-exim4-gmail

  2. November 7, 2010 at 5:20 am

    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)


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: