How to get PHP mail working on Ubuntu 18.04.4

How to get PHP mail working on Ubuntu 18.04.4

Ubuntu OS doesn’t come with an application to send emails by default. On this tutorial, you will learn how to set up an email application on your Ubuntu Sever to be used on WordPress and in other applications.
 

Prerequisites:

Ubuntu 18.04.4 server installed, PHP and Fully Qualified Domain Name.

 

1. Install Postfix

Let’s update the package database first.

sudo apt-get update

Install mailutils, which will automatically install Postfix.

sudo apt install -y mailutils

On the first Postfix configuration screen, select OK by pressing TAB and ENTER
postfix-config-1

Select Internet Site and press ENTER.
postfix-config-2

System mail name should be your domain name eg. example.com, press ENTER.
postfix-config-3
Package should now be installed.
 

2. Configure Postfix

For security reasons, you should instruct Postfix only to process requests to send emails from the server on which it is running.

Edit the Postfix configuration file.

sudo nano /etc/postfix/main.cf

Towards the bottom of the file, find the line inet_interfaces = all. (Press CTRL + W to search)
Change it to:

inet_interfaces = loopback-only

Save file and exit. (Press CTRL + X, press Y and then press ENTER)

Lastly, let’s restart Postfix.

sudo systemctl restart postfix

 

3. Test Postfix

We’ll now send a test email message. Make sure to replace test@example.com with your own email address.

echo "Test Email message body" | mail -s "Email test subject" test@example.com

Don’t forget to check your spam folder.
If you still haven’t received any mail after a few minutes, check the mail error log.

sudo tail /var/log/mail.log

If the mail log is empty or doesn’t give enough information, try parsing the syslog. This will return the last 50 entries for postfix.

sudo tail -f -n 50 /var/log/syslog | grep postfix

If the syslog is empty and you still haven’t received any test email, it’s possible that the test email was rejected by the recipient server. You should check to see if anything has bounced back to your mail folder.

sudo less /var/mail/$(whoami)

Press uppercase G to scroll to the bottom of the file and lowercase q to quit. The $(whoami) variable returns the currently logged in user.
 

4. Test PHP mail()

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

ABOUT AUTHOR

Nuno

I am a Freelance Web Developer and WordPress Expert based in London with a wealth of website development and support experience. I am great at problem solving and developing quick solutions.

LEAVE A COMMENT

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • LET'S TALK!

    Fill in the form below to make an enquiry or find my contact details on my contact page.