top of page
Search
Writer's pictureTUWA SOLUTIONS

Configure Postfix to Send Email Using External SMTP Servers

Updated: Jan 26, 2022

Following procedure was deployed with Redhat Linux 6 and 7:

If you want to configure Postfix to use an external SMTP provider like Mandrill, and SendGrid so you no longer have to build, maintain, and scale your own SMTP relay server. Another reason is to avoid getting your mail flagged as spam if your current server’s IP has been added to a spam list. This guide shows you how to configure Postfix to use an external SMTP provider and also shows specific examples for Mandrill, and SendGrid. However, you can apply the steps in this guide to configure Postfix to use any external SMTP provider.

What is Postfix Used For?

Postfix allows you to route and deliver emails and uses the Simple Mail Transfer Protocol (SMTP).

What is the Postfix Configuration Process?

The majority of the Postfix configuration process is completed in the main.cf and master.cf files that are located in the /etc/postfix/ directory. The /etc/postfix directory is available once you have installed Postfix on your Linux system.

At a high-level Postfix configuration involves the following steps:

  1. Gather prerequisites, which include a fully qualified domain name(FQDN), updating your system, and installing the libsasl2-module package

  2. Install Postfix on your system

  3. Configure SMTP usernames and passwords

  4. Secure your password and hash database files

  5. Configure the relay server

  6. Test your Postfix configuration

  7. Set up Postfix with Mandrill, and SendGrid

Postfix Configuration

Gathering Prerequisites

In this section, you complete all prerequisite steps to configure Postfix to use an external SMTP server. Prerequisites to configure Postfix using an external SMTP server are:

  1. Ensure you have fully qualified domain name (FQDN). The Fully Qualified Domain Name (FQDN) is the absolute domain name, including subdomains, top-level domain, and root zone, that will direct queries under the Domain Name System (DNS) to an exact location


Ensure your system is up-to-date:
 yum update -y 
Install the libsasl2-modules package:
 yum install libsasl2-modules

Your system is now ready to install Postfix.

Installing Postfix


Install Postfix by running the following command:
yum install postfix

Once the installation is complete, open the /etc/postfix/main.cf file using your preferred text editor. Edit the file to add your Linode’s FQDN to the myhostname configuration, if it is not already configured, and save your changes.

File: /etc/postfix/main.cf
1 myhostname = fqdn.domain.com

Configuring SMTP Usernames and Passwords

Usernames and passwords are stored in the /etc/postfix/sasl_passwd file. In this section, you add your external mail provider credentials to the sasl_passwd Postfix configuration file.

First, open or create the /etc/postfix/sasl_passwd file:

  1. Using a text editor of your choice, open the /etc/postfix/sasl_passwd file. If it does not yet exist, create it.

Add the example line to your sasl_passwd file and replace username and password with your SMTP provider credentials.

File: /etc/postfix/sasl_passwd1     [mail.isp.example] username:password

Create a Hash database file for Postfix using the postmap command. This command creates a new file named sasl_passwd.db in the /etc/postfix/ directory.
 sudo postmap /etc/postfix/sasl_passwd 

Secure your Password and Hash DB files

In the previous section you added plain text credentials to the /etc/postfix/sasl_passwd and /etc/postfix/sasl_passwd.db files. For this reason, you have to change the each file’s permissions to restrict access to all users other than the root user.

Run the following command to change your each file’s permissions:
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db  
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db 

Configuring the Relay Server

You are now ready to provide the configurations needed by Postfix to use the external SMTP server. This configuration tells Postfix to deliver mail indirectly via a relay host, which in this case, is an external SMTP server.

  1. Using a text editor, open the /etc/postfix/main.cf file.

  2. Update the relayhost configuration with your external SMTP relay host. Replace mail.isp.example with your provider’s information. If you specified a non-default TCP port in the sasl_passwd file, then use the same port when configuring the relay host. The example uses 587 as its port number.

File: /etc/postfix/main.cf1 2 # specify SMTP relay hostrelayhost = [mail.isp.example]:587
  1. Add the end of the file, add the example file’s parameters to enable authentication and save the changes you made to your main.cf file. File: /etc/postfix/main.cf 1 2 3 4 5 6 7 8 9 10 # enable SASL authenticationsmtp_sasl_auth_enable = yes # disallow methods that allow anonymous authentication.smtp_sasl_security_options = noanonymous # where to find sasl_passwdsmtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd # Enable STARTTLS encryptionsmtp_use_tls = yes # where to find CA certificatessmtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Restart Postfix to enable your configurations:
 sudo service postfix restart 

Testing Your Postfix Configuration

You can now test your Postfix Configurations by using your system’s mail utility.

  1. Install the mailutils package to use the mail utility. sudo apt-get install mailutils

  2. Compose an email to verify that your system is able to successfully send it. Replace the email address with your own and your intended recipient’s email address. echo "body of your email" | mail -s "This is a subject" -a "From:you@example.com"

Postfix Configuration with Mandrill, and SendGrid

This section shows you settings for some popular mail services you can use as external SMTP servers. You may have to do some fine-tuning on your own to avoid Postfix logins being flagged as suspicious.

Postfix Configuration for Mandrill

  1. Open your /etc/postfix/sasl_passwd file and replace USERNAME and API_KEY with your own Mandrill credentials and save your changes. File: /etc/postfix/sasl_passwd1 [smtp.mandrillapp.com]:587 USERNAME:API_KEY

  2. Open your /etc/postfix/main.cf file and add the Mandrill relay host information included in the example file. File: /etc/postfix/main.cf1 relayhost = [smtp.mandrillapp.com]:587

  3. Create a hash database file for Postfix using the postmap command: sudo postmap /etc/postfix/sasl_passwd

  4. Restart Postfix to enable your new configurations: sudo service postfix restart

Postfix Configuration for SendGrid

  1. Open your /etc/postfix/sasl_passwd file and replace USERNAME and PASSWORD with your own SendGrid credentials and save your changes. File: /etc/postfix/sasl_passwd1 [smtp.sendgrid.net]:587 USERNAME:PASSWORD

  2. Open your /etc/postfix/main.cf file and add the SendGrid relay host information included in the example file. File: /etc/postfix/main.cf1 relayhost = [smtp.sendgrid.net]:587

Create a hash database file for Postfix using the postmap command:
sudo postmap /etc/postfix/sasl_passwd 
Restart Postfix to enable your new configurations:
sudo service postfix restart 

How Do I Check My Postfix Configuration?

All Postfix configuration files are stored in the /etc/postfix directory. You can use the postconf command to view all of your system’s Postfix configurations and their details. This is a great way to verify that your Postfix configuration values are as you expect them to be.

To view all the available postconf options, view its manual pages using the man postconf command.



0 views0 comments

Comentários


bottom of page