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
- Ubuntu 20.04;
- Domain name;
- DNS A record, containing the public IP address of your domain;
- TLS certificate for e-mail encryption;
Step 1 – Installing Postfix
The easiest way is to install mailutils package, which contains Postfix and some other packages.
During installation, a window for configuring Postfix will appear.
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.
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.
inet_interfaces = loopback-only
Also adjust the mydestination parameter as follows.
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.
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.
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.
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.
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:
Add a line to the end of the file. In this example, we will configure sending system messages to mail@domain.com
Save the file and close it. To apply the changes, enter the command: