How To Get PHP mail() Working On Ubuntu 20.04, 18.04 & 16.04

Last updated on

In this article we are going to install and configure Postfix to allow our applications to use the PHP mail() function. Tested and working on Ubuntu 20.04, 18.04 & 16.04.

 

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. example.com, press ENTER.

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:

/etc/postfix/main.cf
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

If you intend on sending email to your own domain, Postfix requires some additional configuration. For example, I want my PHP app to send emails to [email protected] This will fail if you don’t make some additional changes to your main.cf file.

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 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.

Update Feb 2020: If you are sending to Gmail, Outlook or other large email providers, you may get a bounce error similar to “This message does not have authentication information or fails”. If so, please see article:

4. Test PHP mail()

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

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

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

Mail Never Received / Spam Issues

If emails are being rejected by the remote mail provider (such as Gmail), or mail is going straight to your email client’s spam folder, you may need to do some additional configuration on your domain (SPF, DKIM, DMARC records) to get past spam filters. Please see:

Alternatively, you could relay messages through an external SMTP server of your choice or through Gmail’s SMTP server.

1 Star2 Stars3 Stars4 Stars5 Stars 4.56 (34 votes)

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

25 replies

avatar
  Subscribe  
newest oldest
Notify of
shady
Guest
shady
shady
14 days ago

postfix/smtp[21099]: connect to mx01.mail.icloud.com[17.57.154.6]:25: Connection timed out im seeing this in AWS

Tom Veatch
Guest
Tom Veatch
Tom Veatch
24 days ago

Ubuntu 16.04 in July 2020:
ignores -y in “sudo apt install -y mailutils” (man apt lacks mention of -y)
the postfix configuration screen fails to appear.
main.cf does not exist in /etc/postfix (instead it’s main.cf.proto)
Same findings after sudo apt-get install mailutils”

notabadusername
Guest
notabadusername
notabadusername
3 months ago

Thank you so much! Solved my mail problems 🙂

sander
Guest
sander
sander
7 months ago

Hi got this message:
host ASPMX.L.GOOGLE.COM[2a00:1450:400c:c09::1a] said: 550-5.7.1 [2a00:6d40:72:6d28::1] Our system has detected that this message does 550-5.7.1 not meet IPv6 sending guidelines regarding PTR records and 550-5.7.1 authentication. Please review 550-5.7.1 https://support.google.com/mail/?p=IPv6AuthError for more information 550 5.7.1 . o7si15131076wrm.214 – gsmtp (in reply to end of DATA command))

Any idea how to solve this?

Abizera Oreste
Guest
Abizera Oreste
Abizera Oreste
10 months ago

Thanks

Max Kulik
Guest
Max Kulik
Max Kulik
11 months ago

This worked for me to get my WordPress site to send contact form messages to our e-mail address! Thanks so much for the help!

klaus
Guest
klaus
klaus
1 year ago

excellent! Thanks so much, sved me some headaches ..
Klaus

fm.8bit
Guest
fm.8bit
fm.8bit
1 year ago

Thanksss 👏👏👏👏👏

Galen Currah
Guest
Galen Currah
Galen Currah
1 year ago

Ubuntu Server 18.04, October 2018
“Package ‘mailutils’ has no installation candidate”

padyp
Guest
padyp
padyp
1 year ago

This also works with Ubuntu 18.04

Nikhil Sharma
Guest
Nikhil Sharma
Nikhil Sharma
2 years ago

Great Post thanks. Simple and easy to follow.

crisdale
Guest
crisdale
crisdale
2 years ago

Hey. When are you going to post article for ” How to get PHP mail() working on Ubuntu “18.04”

tilito
Guest
tilito
tilito
2 years ago

Thanks, it works for Ubuntu 17.10 and 7.1 too 🙂

Lunny
Guest
Lunny
Lunny
2 years ago

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

This is the mail system at host example.com.

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 gmail-smtp-in.l.google.com[108.177.119.26] said:
550-5.7.1 [128.199.51.22 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
https://support.google.com/mail/?p=UnsolicitedIPError to review our 550
5.7.1 Bulk Email Senders Guidelines. x4si1208017edl.361 – gsmtp (in reply
to end of DATA command)

jeffg
Guest
jeffg
jeffg
2 years ago

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

Keith.garad
Guest
Keith.garad
Keith.garad
2 years ago

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

using ubuntu 17.10 & php7.1

Jordan
Guest
Jordan
Jordan
8 months ago

Same issue, postfix is not installed for me on Ubuntu 18.04 LTS.

Cataclysma
Guest
Cataclysma
Cataclysma
1 month ago

sudo apt-get install postfix