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.
Update the local package index.
$ sudo apt update
Install mailutils, which includes Postfix.
$ sudo apt install mailutils
During installation you will see a series of interactive configuration screens for Postfix. Fill the prompts with the following information:
Internet Siteby pressing TAB then ENTER to confirm.
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
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
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.
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`
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.
Create a file called sasl_passwd which will store the Zoho credentials for Postfix.
$ sudo nano /etc/postfix/sasl_passwd
Populate the file with the details below. Replace username and password with your Zoho account email and password.
If you have Two Factor Authentication enabled on your account, you may have to set up an Application-specific Password.
Create a hash database file for Postfix with the postmap command:
$ sudo postmap /etc/postfix/sasl_passwd
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
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.
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.
Open the configuration file.
$ sudo nano /etc/postfix/main.cf
Locate the line beginning with myhostname = and replace it with:
$ myhostname = localhost
Do the same with the line beginning with mydestination =, replacing it with:
$ mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
Restart Postfix to apply the changes.
$ sudo systemctl restart postfix
Send a test message using the mail command. Replace you@yourdomain with your own email and email@example.com with the recipient.
$ echo "Test email body" | mail -s "Test Subject" -a "From: you@yourdomain" firstname.lastname@example.org
Don’t forget to check your spam folder for this email.
You have successfully set up your Ubuntu 20.04 server to send email from your custom domain.