This is a short guide covers how to enable the mail() function in PHP on Ubuntu.

Here are the steps I took to get sendmail working on my Ubuntu server.

Step 1: Install sendmail

The first thing you need to do is install the sendmail package. Do this by running the following command in your terminal:

$ sudo apt-get install sendmail

Sendmail might already be installed, but just in case, enter above into your terminal:

Step 2: Configure sendmail

After the previous command finishes installing the sendmail package, you need to configure it. You can do that by running the following command, which will configure the mail server:

$ sudo sendmailconfig

You need to choose Yes to every question or otherwise configure it as you see fit.

Step 3 (Optional): Edit hosts file

To edit your server's hosts file, access it by entering the following command into the terminal:

$ sudo vim /etc/hosts

Step 4: Restart Web Server

At this point emails sent via PHP's mail() function should be sending successfully without much delay. It is not necessary to restart server, but let's restart server just for good measure.

For Apache Server, you can enter the following command to restart the server.

$ sudo service apache2 restart

For Nginx Server, you can enter the following command to restart the server:

$ sudo service nginx restart

That's it! You can now use the mail() function in your PHP app.. I hope this solved the problem for you.

References & Resources