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:
- Internet distribution (SMTP)
- Name of the system e-mail: the name of the computer (or any name, just avoid funny characters)
- IP addresses: 127.0.0.1
- Other destinations: localhost
- Relays: leave empty
- Relays 2: leave empty
- Minimize DNS requests DNS: yes (it is a development machine, not a server)
- Maildir Format (we’re developers, we want things made easy)
- Split the configuration: No (there’s not much in there anyway)
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(‘email@example.com’,’hello’,’my message reached you’); this should send the message correctly to the firstname.lastname@example.org 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.