A mail server is very useful when you own a website or web application and want to send transactional emails to users. Postfix is a free and open-source Mail Transfer Agent used to send and receive emails. Postfix is absolutely free, so you don’t need to rely on third-party service providers like Sendgrid or Pepipost. You can easily install and configure Postfix to send emails through your local application.
In this post, we will show you how to install and configure Postfix as a Send-Only SMTP server on Ubuntu 20.04.
- An Ubuntu 20.04 server on the Atlantic.Net Cloud Platform
- A root password configured on your server
Step 1 – Create Atlantic.Net Cloud Server
First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 20.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.
Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.
apt-get update -y
Step 2 – Setup Hostname
Before starting, you will need to set up a fully qualified hostname on your server. You can set up it with the following command:
hostnamectl set-hostname email.linuxbuz.com
Next, edit /etc/hosts file and add the following line:
Add the following line:
Save and close the file when you are finished.
Step 3 – Install Postfix
The simple and easiest way to install Postfix is to install mailutils package to your system.
You can install the mailutils package using the following command:
apt-get install mailutils -y
During the installation, you will be asked to select the type of mail configuration as shown below:
Select Internet Site and hit Enter. You will be asked to provide your mail name as shown below:
Provide your domain name and hit Enter to finish the installation.
Next, check the status of Postfix using the following command:
systemctl status postfix
Step 4 – Configure Postfix as a Send-Only SMTP Server
Next, you will need to configure Postfix to send an email from the local host. You can configure it by editing the Postfix main configuration file:
Change the following line:
inet_interfaces = loopback-only
Save and close the file, then set the hostname directly in the Postfix configuration file using the following command:
postconf -e "myhostname = email.linuxbuz.com"
Next, verify your configured domain name using the following command:
You should get the following output:
mydomain = email.linuxbuz.com
You will also need to verify the default domain name that is appended to sender and recipient addresses.
myorigin = /etc/mailname
Next, display the content of the above file using the following command:
You should see your domain in the following output:
Finally, restart the Postfix service to apply the changes:
systemctl restart postfix
You can also check the status of Postfix with the following command:
systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/lib/systemd/system/postfix.service; enabled; vendor preset: enabled)
Active: active (exited) since Sun 2021-08-15 12:52:44 UTC; 9s ago
Process: 4230 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 4230 (code=exited, status=0/SUCCESS)
Aug 15 12:52:44 ubuntu2004 systemd: Starting Postfix Mail Transport Agent...
Aug 15 12:52:44 ubuntu2004 systemd: Finished Postfix Mail Transport Agent.
Step 5 – Verify Postfix Server
At this point, Postfix is installed and configured as a send-only SMTP server. Now, it’s time to send emails to an external email account.
You can use the following command to send a simple email to the external email address:
echo "This is the body of the email" | mail -s "This is the subject line" [email protected]
You should now see the message in your Inbox or Spam folder.
Step 6 – Forward System Mail
It is a good idea to forward all system mail to your external email address. In this section, we will set up email forwarding for the root user.
Edit the /etc/aliases file:
Find the following line:
And replace it with the following line:
root: [email protected]
Save and close the file, then run the following command to apply the changes:
Now, verify email forwarding by sending an email to the root user:
echo "This new email" | mail -s "This is new email" root
If everything is fine, you should receive an email at your external email address.
Congratulations! You have successfully set up a Postfix as a send-only SMTP server. You can now use this setup with your application to notify your users via email. Give it a try on your VPS from Atlantic.Net!