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
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
W to search)
Change it to:/etc/postfix/main.cf
inet_interfaces = loopback-only
Save file and exit. (Press
Y and then press
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
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)
4. Test PHP mail()
If Postfix is working correctly, you should now be able to send mail via PHP
<?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); ?>