When running your own web server to host applications like WordPress, you will need to install a Mail Transfer Agent (MTA) like Postfix.
If the server is only for web hosting, you don’t want external servers being able to connect via SMTP. In this tutorial, we’ll install Postfix on Ubuntu 16.04 and configure it so that only local applications can send outbound email.
The first thing we need to do is install Postfix which can be done by running the following command.
sudo apt-get install postfix -y
During the installation, you will be presented with the Postfix Configuration screen.
Select Internet Site, then OK and then press Enter.
In the next step of the Postfix Configuration you will be asked to set the System mail name. This should be a domain name that you own and would normally be the domain name of your company mail server.
Enter the domain name you want to use, select OK and then press Enter.
Postfix is now installed and setup to send and receive email. In the next step we will make it so that the server can only send outbound email.
Configure Postfix as a Send-Only SMTP Server
Since we are installing Postfix on a web server, we don’t want it to receive email. All we want to use Postfix for, is for applications like PHP and WordPress to send outbound email. To change Postfix to a Send-Only SMTP server follow the steps below.
Step 1: Open the Postfix global configuartion file.
sudo vim /etc/postfix/main.cf
Step 2: Change the inet_interfaces option from all to localhost and then save the file. For example
inet_interfaces = localhost
Step 3: Make sure emails don’t bounce when an application sends an email to a user in the domain set in the System mail name. This can be done by changing the mydestination option to blank, For example.
Step 4: Restart Postfix to apply the changes.
sudo service postfix restart
That’s it, Postfix is now installed and configured as a Send-Only SMTP server. You can test this out by creating a PHP script that calls the mail() function.