Use Zoho as External SMTP Server with Postfix on Ubuntu 20.04

Updated on July 20, 2020
Use Zoho as External SMTP Server with Postfix on Ubuntu 20.04 header image

Introduction

This article will show you how to configure Postfix to send mail through Zoho’s SMTP server. Using an external SMTP server is useful if your emails are being marked as spam when sending directly from your server. This guide uses Zoho’s free plan for hosting email with your own custom domain.

Prerequisites

1. Install Postfix

  1. Update the local package index.

     $ sudo apt update
  2. Install mailutils, which includes Postfix.

     $ sudo apt install mailutils
  3. During installation you will see a series of interactive configuration screens for Postfix. Fill the prompts with the following information:

    • General type of mail configuration: Choose Internet Site by pressing Tab then Enter to confirm.
    • System mail name: This is your Fully Qualified Domain Name (FQDN) that you wish to send mail from, enter it here.

    Postfix will now complete the installation using the details you provided. If you ever need to get back to this, you can do so by typing:

     $ sudo dpkg-reconfigure postfix

2. Configure Postfix

  1. Configure Postfix to use Zoho’s SMTP server for sending mail. Begin by opening the Postfix configuration file in a text editor.

     $ sudo nano /etc/postfix/main.cf
  2. Find the line relayhost = located 6 lines up from the bottom of the file, and change it to:

     relayhost = [smtp.zoho.eu]:587

    > Note: Depending on your location, .eu may not be the correct country code. Replace with whatever country code that's at the end of the Zoho MX records you created when you set up the account.

  3. Append the following to the end of the file:

     # enable SASL authentication
     smtp_sasl_auth_enable = yes
     # location of sasl_passwd
     smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
     # disallow methods that allow anonymous authentication
     smtp_sasl_security_options = noanonymous
     # location of CA certificate
     smtp_tls_CAfile = /etc/postfix/cacert.pem
     # enable TLS encryption
     smtp_use_tls = yes`
  4. Save and close the file. This tells Postfix to relay mail through the external SMTP server and configures it to use TLS to securely connect to your Zoho account.

3. Configure Username and Password

  1. Create a file called sasl_passwd which will store the Zoho credentials for Postfix.

     $ sudo nano /etc/postfix/sasl_passwd
  2. Populate the file with the details below. Replace username and password with your Zoho account email and password.

     [smtp.zoho.eu]:587 username:password

    > If you have Two Factor Authentication enabled on your account, you may have to set up an Application-specific Password.

  3. Create a hash database file for Postfix with the postmap command:

     $ sudo postmap /etc/postfix/sasl_passwd
  4. You should now have a file called sasl_passwd.db in the /etc/postfix/ directory. These files contain your SMTP credentials in plain text, so ensure only the root user can read or write to them.

     $ 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
  5. Create the certificate for Postfix.

     $ cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem

You should now have a certificate in /etc/postfix called cacert.pem.

4. (Optional) Ensure Postfix Delivers Mail to Your Own Domain

If you’re going to use Postfix to send mail to the same domain as it’s sending from, we need to force Postfix to always use the external SMTP server.

  1. Open the configuration file.

     $ sudo nano /etc/postfix/main.cf
  2. Locate the line beginning with myhostname = and replace it with:

     $ myhostname = localhost
  3. Do the same with the line beginning with mydestination =, replacing it with:

     $ mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
  4. Restart Postfix to apply the changes.

     $ sudo systemctl restart postfix

5. Test Your Configuration

Send a test message using the mail command. Replace you@yourdomain with your own email and recipient@example.com with the recipient.

$ echo "Test email body" | mail -s "Test Subject" -a "From: you@yourdomain" recipient@example.com

Don’t forget to check your spam folder for this email.

Conclusion

You have successfully set up your Ubuntu 20.04 server to send email from your custom domain.