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
- An Ubuntu 20.04 server configured with a Fully Qualified Domain Name (FQDN).
- A non-root user with sudo privileges.
- A Zoho Mail account using the same FQDN as the server.
- Follow our best practices guide to update the Ubuntu server.
1. Install Postfix
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:
- 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
- General type of mail configuration: Choose
2. Configure 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.
3. Configure Username and Password
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.
[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.
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.
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.
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
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.