How to send mail from apache2

To send mail from apache2 using the PHP mail() function requires a Mail Transfer Agent to be installed. Several exists, I use postfix.

The following assumes you can access your web server at ubuntu.dev. This will be the case if you a setup as described for the host. Replace with your hostname if you have an other installation.

How to install and configure the Mail Transfer Agent is described detailed in the Ubuntu postfix guide. Unless you have some specific needs, don’t bother to read it, most is about setting up parameters, but defaults works fine.

The short version is to install the packages postfix and mail-stack-delivery with the commands:

sudo apt-get install postfix
sudo apt-get install mail-stack-delivery

During installation you will be prompted for a few parameters, where the values should be quite obvious from the questions.

This is scripted in install-postfix.sh, where the required parameters are also supplied.

To send an email from your web-site you make a call in your php code like:

mail('receiver@domain', 'mail subject', 'mail body');

As starting template or to test that mail can be send, use testmail.php, which you activate from your browser with

http://ubuntu.dev/testmail.php?to=receiver@domain&subject=header&message=body

Replace receiver@domain, header and body with your values.

Leave a Reply

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