Configuring postfix to relay email through Zoho Mail 5


This has taken me quite some time to figure out.

First of all you need the Zoho email address you want to use when relaying emails through Zoho.

It has to be one of the email addresses you configured by using Zoho control panel. In my case I created one to use only to relay email.

Let’s say that this email address is application@example.com. It will have a password as well, say applicationpassword.

When configuring postfix, you edit many files. Let’s see them one by one.

Generic

The file /etc/postfix/generic maps local users to email addresses.

If email is sent to a local user such root, the address will be replaced with the one you specify.

In my case I have a single line like:

root application@example.com

After editing this file remember to use the command:

  1. postmap generic

Password

The file /etc/postfix/password contains the passwords postfix has to use to connect to the smtp server.

It’s content will be something like:

smtp.zoho.com:587 application@example.com:applicationpassword

You need to do postmap password.

tls_policy

The file /etc/postfix/tls_policy contains the policies to be used when sending encrypted emails by using the TLS protocol, the one I’m using in this case.

The file contains just this line:

smtp.zoho.com:587 encrypt

By doing so we force the use of TLS every time we send an email.

You need to do postmap tls_policy.

smtp_header_checks

The file /etc/postfix/smtp_header_checks contains rules to be used to rewrite the headers of the emails about to be sent.

This is the most important file in our case.

It rewrites the sender so that it always matches our Zoho account, application@example.com.

No more ‘Relaying disallowed’ errors!

This is its content:

  1. /^From:.*/ REPLACE From: LOCALHOST System <application@emanuelesantanche.com>;

No need for postmap here.

You need to install the package postfix-pcre otherwise no rewriting will happen.

  1. apt-get install postfix-pcre

Main.cf

This is the main configuration file postfix uses.

Replace yourhostname with the hostname of your server, the one where postfix is installed on and that is sending emails through Zoho.

  1. # TLS parameters
  2. smtp_tls_policy_maps = hash:/etc/postfix/tls_policy
  3. smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
  4. smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
  5. smtpd_use_tls=yes
  6. smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
  7. smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
  8. smtp_header_checks = pcre:/etc/postfix/smtp_header_checks
  9.  
  10. myhostname = yourhostname
  11. alias_maps = hash:/etc/aliases
  12. alias_database = hash:/etc/aliases
  13. mydestination = yourhostname, localhost.com, localhost
  14. relayhost = smtp.zoho.com:587
  15. smtp_sasl_auth_enable = yes
  16. smtp_sasl_password_maps = hash:/etc/postfix/password
  17. smtp_sasl_security_options =
  18. smtp_generic_maps = hash:/etc/postfix/generic

master.cf

In the file /etc/postfix/master.cf I uncommented this line:

  1. smtps inet n - - - - smtpd

5 thoughts on “Configuring postfix to relay email through Zoho Mail

  • florin

    thx, very useful. I was so close to getting it right, but still zoho was complaining.
    thx to your post, i found the error and now it works perfectly!

  • Gery

    Very useful indeed, thanks!! It worked!! just needed to change `/etc/postfix/smtp_header_checks` from:

    /^From:.*/ REPLACE From: LOCALHOST System ;

    to:

    /^From:.*/ REPLACE From:info@g3eo.com

    and great! check that I had to delete the semi-colon at the end as well as the whole “LOCALHOST SYTEM” plus blank spaces, and that was it!! hope this helps someone else out there, cheers!

  • CroModder

    Thanks for sharing. Not sure which part I was missing(probably smtp_header_checks), but anyway, now it’s working, and I didn’t changed anything in master.cf

  • Roshan

    Hi, I’m kind of a stuck here i have used Gmail which was work fine, but not working for Zoho, below my cf file,can you check where the issue.

    # TLS parameters
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy
    smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
    smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    smtpd_use_tls=yes
    smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
    smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
    #smtp_header_checks = pcre:/etc/postfix/smtp_header_checks
    # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
    # information on enabling SSL in the smtp client.

    #smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
    myhostname = pad10.net
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    myorigin = /etc/mailname
    mydestination = pad10.net, localhost.com, localhost
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mailbox_command = procmail -a “$EXTENSION”
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = loopback-only
    inet_protocols = ipv4

    #relayhost = [smtp.gmail.com]:587
    relayhost = [smtp.zoho.com]:587
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    smtp_tls_CAfile = /etc/postfix/cacert.pem
    smtp_use_tls = yes
    smtp_generic_maps = hash:/etc/postfix/generic

    • esantanche Post author

      You may want to try to add this line to your Main.cf file:

      smtp_header_checks = pcre:/etc/postfix/smtp_header_checks

      And you need the file /etc/postfix/smtp_header_checks.

      It’s content should be something similar to:

      /^From:.*/ REPLACE From: LOCALHOST System <application@emanuelesantanche.com>;

      By doing this the sender will always match your Zoho account and there will be no ‘Relaying disallowed’ errors any more.

      The email application@emanuelesantanche.com is be the one I used to create my Zoho account.

      Another difference I spotted is this one:

      relayhost = smtp.zoho.com:587

      Your relayhost line has a different syntax.

      Maybe this is a problem.

Comments are closed.