If you’re managing a Linux server, you may be wondering how to configure Postfix to send emails from external SMTP servers. Postfix is a popular open-source mail transfer agent (MTA) used to route and deliver email on Linux. While setting up Postfix to send and receive email is not difficult, using external SMTP servers can be a bit more complicated.

In this article, we’ll walk you through how to configure Postfix to send emails from external SMTP servers.

What is Postfix Relayhost?

Postfix relayhost is a configuration directive that tells Postfix which external SMTP server to use when sending outbound emails. This configuration is necessary when you’re sending emails from your Linux server to external domains. Without a relayhost configured, Postfix won’t be able to send emails to external domains.

When configuring Postfix relayhost, you must specify the hostname or IP address of the remote SMTP server you want to use. This may be the hostname or IP address of the SMTP server provided by your ISP, or the hostname or IP address of a third-party SMTP service such as SendGrid, Mailgun, or Amazon SES.

Configuring Postfix to Send Email from External SMTP Servers

Before you can configure Postfix to send emails from external SMTP servers, you’ll need to install Postfix on your Linux server. If you don’t already have Postfix installed, you can install it using your Linux distribution’s package manager.

Once Postfix is installed, you can configure it to send email from external SMTP servers by editing the main Postfix configuration file, /etc/postfix/main.cf. You’ll need to add the following directive to the configuration file:

sudo postconf -e "relayhost = smtp.example.com:587" 

Replace smtp.example.com:587 with the hostname or IP address of the remote SMTP server you want to use.

Configure Authentication for Postfix Relayhost

Once you’ve added the relayhost directive to your Postfix configuration file, you’ll need to configure authentication for the remote SMTP server. This is necessary if the remote SMTP server requires authentication before it will accept and deliver emails.

To authenticate with the remote SMTP server, you’ll need to add the following directives to the Postfix configuration file:

sudo postconf -e "smtp_sasl_auth_enable = yes"
sudo postconf -e "smtp_sasl_password_maps = hash:/etc/postfix/smtp_sasl_password"
sudo postconf -e "smtp_sasl_security_options = noanonymous"

You’ll also need to create a smtp_sasl_password file in the /etc/postfix directory with the following format:

[smtp.example.com]   username:password

Replace smtp.example.com with the hostname or IP address of the remote SMTP server, and replace the username and password with the authentication credentials for the remote SMTP server.

Once you’ve added the authentication credentials, you’ll need to create a smtp_sasl_password.db file by running the postmap command:

sudo postmap /etc/postfix/smtp_sasl_password  

Finally, restart the Postfix service to apply changes.

sudo systemctl restart postfix

Configuring Postfix to Use Multiple SMTP Servers

If you need to send emails from multiple SMTP servers, you can configure Postfix to use multiple relayhosts by adding multiple relayhost directives to the Postfix configuration file. For example:

relayhost = [smtp.example.com] 
relayhost = [smtp2.example.com]

You’ll also need to configure authentication credentials for each SMTP server. This can be done by adding multiple entries to the smtp_sasl_password file.

Conclusion

Configuring Postfix to send emails from external SMTP servers can be a bit complicated, but with the right configuration settings, you can easily set up Postfix to use remote SMTP servers for sending outbound emails. In this article, we’ve walked you through how to configure Postfix to send emails from external SMTP servers, as well as how to configure Postfix to use multiple SMTP servers. If you have any questions, feel free to leave a comment below.

Happy sending!