Todd Austin

Personal Blog

Using the PHP mail() function on Ubuntu 14.04

11 months ago · 1 MIN READ
#PHP  #Ubuntu 

Alright, I'll start off by saying that I know there are better and more professional solutions for sending email on web servers. But sometimes you just want to use PHP's mail() function to get it done quickly and simply. The following tutorial will walk you through the installation and setup process of Ubuntu's sendmail package.

Step 1: Install sendmail

Sendmail might already be installed, but just in case, enter this into the server terminal:

sudo apt-get install sendmail

Step 2: Configure sendmail

Enter the following command into the terminal:

sudo sendmailconfig

Type Y for each question it asks.

Step 3: Edit the Hosts File

Time to edit the hosts file on your server. Access it by entering the following command into the terminal:

sudo vim /etc/hosts

Add the following line to the top of the file and save it: localhost localhost.localdomain YOUR_DOMAIN_NAME_HERE.COM

Comment out any duplicate lines that already existed.

Step 4: Restart Apache

Let's restart your server just for good measure.

sudo service apache2 restart

At this point emails sent out via PHP's mail() function should be sending successfully and without much delay.

Step 5: Test the Functionality

This is a handy little script to help test your hosting to ensure you can send emails from PHP based sites which include a contact form or similar. This includes CMS systems such as WordPress and Joomla as well as eCommerce platforms such as Magento.

If you have made changes (eg enabled SMTP in the PHP settings) or moved hosts, this is a quick way to be confident the emails are working.

Create the test-email.php script with the following:

    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );
    $from = "MailTest@YOUR_DOMAIN.COM";
    $subject = "PHP Mail Test script";
    $message = "This is a test to check the PHP Mail functionality";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    echo "Test email sent";

From your server, run the following command:

php test-email.php

That's it! You should be off and running now with a simple PHP mail setup.


Todd Austin

Web artisan with a passion for open source technologies.
comments powered by Disqus

Proudly powered by Canvas · Sign In