How to get PHP mail() working on Ubuntu 16.04 / 17.04

In this article we are going to install and configure Postfix to allow our applications to use the PHP mail() function.

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

Select Internet Site and press ENTER.

System mail name should be your domain name eg., press ENTER.

Package should now be installed.

2. Configure Postfix

Edit the Postfix configuration file.

sudo nano /etc/postfix/

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 [email protected] with your own email address.

echo "Test Email message body" | mail -s "Email test subject" [email protected]

Don’t forget to check your spam folder.

If you still haven’t received any mail, check the mail error log.

sudo tail /var/log/mail.log

If the mail log is empty or doesn’t exist, 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()

If Postfix is working correctly, you should now be able to send mail via PHP mail().

$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: web[email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Spam issues

If emails are going straight to your email client’s spam folder, you may need to consult your domain host about setting up Domain Keys and SPF records. Alternatively, you could relay messages through an external SMTP server of your choice or through Gmail’s SMTP server.

Let me know in the comments if this helped. Follow me @DevAnswers or buy me a beer 🍺


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

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces

7 replies

Thank you for this guide. However, I cannot send to Gmail from my DigitalOcean droplet.

This is the mail system at host

I’m sorry to have to inform you that your message could not
be delivered to one or more recipients. It’s attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

The mail system

: host[] said:
550-5.7.1 [ 1] Our system has detected an unusual rate
of 550-5.7.1 unsolicited mail originating from your IP address. To protect
our 550-5.7.1 users from spam, mail sent from your IP address has been
blocked. 550-5.7.1 Please visit 550-5.7.1 to review our 550
5.7.1 Bulk Email Senders Guidelines. x4si1208017edl.361 – gsmtp (in reply
to end of DATA command)

Thanks I got it working but everything goes straight to Gmail spam box. Is there a way around it?

You’ll need to consult your domain host about setting up Domain Keys and SPF records.

Domain Keys is an e-mail authentication system that allows for incoming mail to be checked against the server it was sent from to verify that the mail has not been modified. This ensures that messages are actually coming from the listed sender and allows abusive messages to be tracked with more ease.

SPF will specify which machines are authorized to send email from your domain. This means that only mail sent through this server will appear as valid mail from your domain when the SPF records are checked.

You could also route mail through an external SMTP server or route mail through Gmail’s SMTP server, but you’ll be limited on how many emails you can send per day.

1) postfix configuration did not appear
so what to do ?
2) wish to send emails externally

using ubuntu 17.10 & php7.1