Table of Contents

Original Content

The original content was found here on 2019-06-21.

Only the important stuff

Here are the postfix settings I found to be important:

/etc/postfix/server.cred

[mail.server.com]:587 USERNAME:PASSWORD

/etc/postfix/main.cf

inet_protocols = ipv4
local_transport = error:local mail delivery is disabled
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_password_maps = hash:/etc/postfix/server.cred
smtp_tls_security_level = encrypt

Then postmap and restart

postmap /etc/postfix/server.cred
service postfix restart

Original Content

There are many reasons why you would want to configure Postfix to send email using an external SMTP provider such as Mandrill, SendGrid, Amazon SES, or any other SMTP server. One reason is to avoid getting your mail flagged as spam if your current server’s IP has been added to a spam list.

In this tutorial, you will learn how to install and configure a Postfix server to send email through Mandrill, or SendGrid. Updated Guide for Gmail and Google AppsPermalink

Before starting this tutorial, you should have:

sudo apt-get update
apt-get install libsasl2-modules

Installing Postfix

In this section, you will install Postfix and set the domain and hostname.

sudo apt-get install postfix
  Select Internet Site.
  sudo nano /etc/postfix/main.cf
  myhostname = fqdn.example.com

Configuring SMTP Usernames and Passwords

Usernames and passwords are generally stored in a file called sasl_passwd in the /etc/postfix/ directory. In this section, you’ll add your external mail provider credentials to this file and to Postfix.

If you want to use Mandrill, or SendGrid as your SMTP provider, you may want to reference the appropriate example while working on this section. For Google Apps and Gmail-specific settings, check out our Configure Postfix to Send Mail Using Gmail and Google Apps on Debian or Ubuntu guide.

sudo nano /etc/postfix/sasl_passwd

[mail.isp.example] username:password

[mail.isp.example]:587 username:password

sudo postmap /etc/postfix/sasl_passwd

If all went well, you should have a new file named sasl_passwd.db in the /etc/postfix/ directory. Securing Your Password and Hash Database FilesPermalink

The /etc/postfix/sasl_passwd and the /etc/postfix/sasl_passwd.db files created in the previous steps contain your SMTP credentials in plain text.

For security reasons, you should change their permissions so that only the root user can read or write to the file. Run the following commands to change the ownership to root and update the permissions for the two files:

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

In this section, you will configure the /etc/postfix/main.cf file to use the external SMTP server.

sudo nano /etc/postfix/main.cf

# specify SMTP relay host

relayhost = [mail.isp.example]:587
* Note: Check the appropriate Google Apps, Mandrill, or SendGrid section for the details to enter here.
* At the end of the file, add the following parameters to enable authentication:
# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
* Save your changes.
* Restart Postfix:
sudo service postfix restart