Forward all incoming emails to other SMTP server or gateway

In this post we will learn how to forward all emails of particular domain to another SMTP server or gateway.
I also used this method in different scenario.

Scenario: I have my own mail server which can host multiple virtual domains and users (postfix+mysql).
Now a client has domain called example.com and for mailing he is using google mail service. Means he has hosted his domain example.com in google apps.
Now client wants to use an email id called feedback@example.com only for sending emails i.e notification emails from my Mail server but wants to recieve and reply emails only from google apps(google mail server).

Here it is simple method which you can use for above given scenraio or only for just forwarding all email to SMTP server or gateway.

Follow the given below steps:

Step 1: Edit the main.cf file and append the line “transport_maps = hash:/etc/postfix/transport” and save/close the main.cf file

vi /etc/postfix/main.cf

transport_maps = hash:/etc/postfix/transport

Step 2: Create a new file /etc/postfix/transport and give the smtp server address.

Syntax inside file /etc/postfix/transport:

domainname smtp:FQDN-of-smtp-server

For my secenario because I have to send all incoming email of example.com to google mail server. I use the command dig mx google.com and selected the highest priority SMTP server FQDN.

Note: Instead of aspmx.l.google.com give SMTP FQDN of your mail server

vi /etc/postfix/transport

example.com smtp:aspmx.l.google.com

Step 3 Reload or restart the postfix

/etc/init.d/postfix reload 

OR

/etc/init.d/postfix restart

Note: for forward all domains email to other SMPT server use wild card (*) in place of domain name in file /etc/postfix/transport

Syntax:

* smtp:FQDN-of-SMTP-Server

Important note: If you have scenario similar to mine

spam email is biggest problem in maintaing the Mail server. If you are already managing the mail server I hope you are also aware about terms like SPF,DKIM and PTR record.

In my scenario I only have to play with DKIM and rest of the thing were already in order.
I am using opendkim and I use opendkim for generateing DKIM key in Mail server.
The only change I did , I changed the name of selector with domain name.(mostly we use selector name as default)

4 thoughts on “Forward all incoming emails to other SMTP server or gateway”

  1. Thanks for this, especially your note on using a wildcard in the transport file.
    I had referred to “man 5 transport” EXAMPLES section where it had:

    my.domain :
    .my.domain :
    * smtp:outbound-relay.my.domain

    Your note appears to have only a single space after the asterisk. I had 2 tabs there originally and nothing was being relayed. I tried a single space, like this:

    * smtp:outbound-relay.my.domain

    and that fixed my problem. I have not seen this documented anywhere.

    Reply
    • I see that what I pasted from the man page got changed on this forum.

      There were about 12 spaces between the asterisk and “smtp:outbound…”

      Reply
  2. The tranport file musst be to transform in a .db file. You need only …

    # postmap /etc/postfix/transport

    … before the reload/restart.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.