How to configure Postfix on Ubuntu 20.04 as your outgoing email server

July 12, 2020
UbuntuPostfix

One of the most common questions you need to answer when setting up a server for your web project is how you are going to be sending emails. There are paid, hosted services that provide email sending capability but they can end up being quite expensive at scale and won’t suit all of the use cases, so you may need to configure an SMTP server yourself from scratch.

This guide is focused on Ubuntu 20.04 however it will most likely also work if you have earlier version of Ubuntu or another Debian based Linux distribution.

Installing Postfix

Postfix is included in the default Ubuntu package repository, so you can utilize apt like you would with other system programs. Remember to update the package list first.

apt-get update
apt-get install postfix

Or, if you are not logged as a root user:

sudo apt-get update
sudo apt-get install postfix

The installer will then ask you a few questions. Make sure you select Internet Site when prompted to specify the general type of mail configuration and type the valid hostname. (e.g. used as a domain part of the email addresses, so we would use stackscout.io and not mail.stackscout.io)

After the installation is finished, Postfix is up and running (you can check by executing service status postfix) but there are additional configuration options worth looking into.

Configuring Postfix

Postfix has several configuration files that you can edit. In order to apply the changes you’ve made, you need to restart the server using service restart postfix.

  • /etc/postfix/main.cf - this is the main configuration file that Postfix reads which defines the parameters and configuration that controls the mail service. If the configuration option is not present in this file, Postfix will assume a default value for it.
  • /etc/postfix/master.cf - Postfix implements its services as daemon processes. These are run in the background and started by the master process. This configuration file tells Postfix which daemon runs when the service is requested and how a client program connects to the service.

Optimizing your Postfix mail server

Performance tuning of your Postfix mail server is a bit of an art and will largely depend on the use case. There are however some universal options you may want to configure to ensure it’s working in an efficient and reliable way.

Basic way of protecting against spam is to disable the VRFY (verify) command, which can be used to check if an email address is valid on the mail server and deliver spam. In normal operation this command is not needed and can be disabled by setting the following option in /etc/postfix/main.cf configuration file.

disable_vrfy_command = yes

If you have a web server running on the same host (e.g. your Laravel or Ruby on Rails application) you will want to change the network interface Postfix is listening on to local only. Edit /etc/postfix/main.cf and update the following option:

inet_interfaces = loopback-only

If you are experiencing slow delivery times on your server, try some of the following options:

  • reduce the time Postfix spends on connecting to non-responding SMTP servers by reducing the values of smtp_connect_timeout and smtp_helo_timeout
  • many performance issues are related to slow DNS lookups, in which case you can run a local name server
  • increase the number of simultaneous deliveries by increasing initial_destination_concurrency and default_destination_concurrency_limit parameters
  • tune the number of Postfix processes, this is set by default_process_limit option in main.cf configuration file but also overriden for specific services in master.cf

Additional commands that interact with Postfix

Check the outgoing mail queue:

postqueue -p

Flush the outgoing mail queue:

postqueue -f

View a message from the queue:

postcat -vq {message id}

Print Postfix queue domain and age distribution:

qshape active

Print Postfix queue domain and age distribution for deferred messages:

qshape deferred

Review the Postfix logs:

cat /var/log/mail.log

Further reading

One book that we can easily recommend if you are interested in a deep-dive into the world of Postfix configuration and optimization is Postfix: The Definitive Guide: A Secure and Easy-to-Use MTA for UNIX Kindle Edition.

StackScout gives you a 24/7 insight into the state of your servers. Start your free trial now.

Start your Free Trial

We use our own and third-party cookies to improve your experience and our services, and to analyse the use of our website. By clicking Accept we take that to mean that you accept our privacy policy.