The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.
In this tutorial, you will learn how to install and configure Postfix on your server, so that other applications installed on that server can use the Postfix to send emails.
Why you need Postfix?
If you are already using a third-party service provider like Sendgrid, Pepipost etc to send emails, then you really don't need a Postfix. But, if you are not using a third party and want to set up something on your local server which can help you to send emails at scale, then you can use Postfix for that purpose.
Postfix is one of the most popular open-source Mail Transfer Agent (MTA) which route and delivers mails.
Note, the hostname of your machine must match your domain name. And, in order to check the same you can run the following command:
The output should show the domain name e.g. mycompany.com which you have pointed to this server.
Just confirm your Debian server version first using the below lsb command:
The output should show Debian 10.
The next step is two install two important packages mailutils, which includes programs necessary for Postfix to function, and the postfix itself.
Let's first update the Package Database itself:
sudo apt update
Next, install mailutils, a package which postfix requires to function properly.
sudo apt install mailutils
Finally, install the postfix package itself:
sudo apt install postfix
Once all the package is downloaded you will see a screen like the one below which will be asking you to choose the type of configuration. Press Enter to see next screen which shows options to select.
Mainly there are 4 configurations
The default option is the Internet Site and that’s the recommended one for this tutorial too. So, press TAB, then ENTER to continue with the installation.
Select Internet Site and press OK. you will be asked to enter the System mail name, which is actually the domain name. Enter your domain (which you mapped to this server earlier at the start of this tutorial) and then press OK.
If it shows a subdomain like subdomain.yourcompany.com, change it to just yourcompany.com. When you’ve finished, press TAB, then ENTER.
Now, your postfix is successfully installed on your Debian 10 machine. Now, the next steps is to configure the Postfix by modifying its settings.
In this step, you will be going to configure Postfix as a send-only SMTP server i.e. it should process requests to send emails only from the server on which is it is running, i.e. from localhost.
For that to happen, Postfix needs to be configured to listen only on the loopback interface, the virtual network interface that the server uses to communicate internally. To make those changes, you need to open the Postfix's main configuration file main.cf. You can use your favourite editor vim or nano to open the same.
sudo nano /etc/postfix/main.cf
You need to make the following changes in the configuration. Locate for the below sections and change as per the instructions below:
inet_interfaces = all to inet_interfaces = loopback-only
Inet_interface in main.cf is used to control addresses postfix listens for incoming addresses, specifying this to loopback-only means that connection can only originate from that machine where postfix is installed.
Once the file is changed, save it. And restart postfix
sudo systemctl restart postfix
Another parameter, you’ll need to modify is mydestination, which is used to specify the list of domains that are delivered via the local_transport mail delivery transport. By default, the values are similar to these:
mydestination = $myhostname, example.com, localhost.com, localhost
Change the above values with the one suggested below:
mydestination = $myhostname, localhost.$your_domain, $your_domain
Save and close the file.
Now you have configured postfix successfully.
Now we’ll test whether our SMTP server can send emails to an external account using mail command of mailutils package.
echo "This is the body of the email" | mail -s "This is the subject line" email_address
Change body and subject line accordingly and in place of email address use valid email id.
Check the inbox or spam folder of your mailbox where you sent the email.
Note that with this configuration, the address in the From field for the test emails you send will be firstname.lastname@example.org, where yourusername is your Linux username and mycompany.com is the domain set in your server’s hostname. If you change your username, the From address will also change.
You can also check the email sending logs in /var/log/mail.log
That's all, you have successfully installed and configured Postfix as send-only SMTP server. Please feel free to share your learnings below in comments. We would love to add those as a part of this tutorial. Further if your go to Configure Postfix Send Mail Using an External SMTP Server which is an extended tutorial catering to Send mail.