07.06.2023

How to Install and Configure Postfix as a Send-Only SMTP Server on Ubuntu 20.04

Postfix is used for sending and receiving emails. There are often situations when the server only needs to send mail. For example, to send notifications from apps. In such cases, the Postfix option for sending messages only is useful.

Initial conditions

Step 1 – Installing Postfix

The easiest way is to install mailutils package, which contains Postfix and some other packages.

sudo apt install mailutils

During installation, a window for configuring Postfix will appear.

Screenshot 1. Postfix Configuration.

An Internet site is the most suitable option in our case. After selecting it, set the System mail name. It must be equal to the hostname and your domain name.

Screenshot 2. Postfix System mail name configuration.

Step 2 - Configuring Postfix

To send emails only from the current server, we only need to listen to the loopback interface. Therefore, in the main.cf file change the “inet_interfaces” parameter to loopback-only.

sudo nano /etc/postfix/main.cf
inet_interfaces = loopback-only

Also adjust the mydestination parameter as follows.

mydestination = localhost.$mydomain, localhost, $myhostname

Restart Postfix.

sudo systemctl restart postfix

The current settings are sufficient for sending emails from the server. Let's test it.

Step 3 – Postfix testing

We will use the mail command to check whether Postfix will send an email.

echo "Fill in some text of the email here" | mail -s "Message subject" recipient's-email-address

Fill in the message text and title. The email will be sent to the specified address instead of the recipient's-email-address.

If the email doesn't arrive, check the Spam folder. If it is not there, then you need to check the configuration. Another cause of problems may be a mismatch between the host name, server name, and your domain name.

Step 4 - Enabling SMTP encryption

Email encryption is very important for security reasons. In addition, some servers consider unencrypted emails to be spam.
To encrypt email, you must specify a valid certificate in the Postfix configuration.

sudo nano /etc/postfix/main.cf

Specify the path to the TLS domain certificate in the smtpd_tls_cert_file field, the private key in the smtpd_tls_key_file, and enter yes in the smtp_use_tls.

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/domain-name.pem
smtpd_tls_key_file=/etc/ssl/private/domain-private-key.pem
smtp_use_tls=yes

Restart Posfix.

sudo systemctl restart postfix

Step 5 - Setting up aliases

Sometimes you need to redirect internal messages for users to the mailbox. For example, to redirect system messages. Aliases are used for this purpose. To configure them, edit the file:

sudo nano /etc/aliases

Add a line to the end of the file. In this example, we will configure sending system messages to mail@domain.com

root: mail.@domain.com

Save the file and close it. To apply the changes, enter the command:

sudo newaliases