Setup Postfix to Relay Mail for Specific Domains or Users

Mail Relaying
I have been working with my mail server a lot lately and I ran into some problems of getting mail delivered to Outlook.com Mail Accounts (Hotmail.com, Outlook.com, Live.com, etc) but I will discuss that more in another article (In Short two months later and still can’t). I have an user that subscribes to MailRoute.net’s excellent spam filtering service, and they use it with my mail server for inbound filtering. However, because Outlook.com will refuse to deliver messages from my mail server, we decided to use MailRoute’s outbound filtering – not really for the outgoing filter, but for the better deliverability to the Outlook.com Mail Accounts.

So I needed to setup my mail server to relay mail for one domain to another server that is outside of my control and network. My mail server uses Postfix 2.10 for the Mail Transfer Agent (MTA). So this is how you can configure Postfix forward (relay) all mail for one virtual domain to another mail server (smart host) before being sent to its final destination.

Setup Mail Relay on Your Mail Server

You will need to do all of this as root, so either sudo or elevate to a root shell.

  1. Create a file that will contain the domains that will be processed by another server. I used /etc/postfix/bysender.
  2. Open the file bysender, add the following information:
    @example.com   [outbound.thirdparty.server.com]
    
    • On the left @example.com is the domain that you want to have its messages forwarded (relayed) to the outside server
    • The right [outbound.thirdparty.server.com] is the server that you want to relay messages to, and yes you need the brackets
    • By the way: You could have one particular user’s messages relayed to a different outside mail server by just putting in the entire e-mail address on the left side
  3. Save and Close the bysender file.
  4. Now we need to hash/map the bysender file so that Postfix can use it. Run the following command:
    postmap /etc/postfix/bysender
  5. Now open your Postfix main.cf and insert the following:
    sender_dependent_relayhost_maps = hash:/etc/postfix/bysender
    
    • Update the file path if needed
  6. Now you need to restart Postfix, run the following command:
    service postfix restart 
    • This assumes Ubuntu’s Service Command

Go ahead and test the configuration by sending a test message from the domain and review the headers at the receiving mail account, you should see the mail being sent to your smart host, or in my case MailRoute, before arriving to the servers for the outside mail account.

Side Notes

I found this information via Zimbra Support documentation, I attempted to find this in the official Postfix documentation, but the docs for Postfix are very hard to understand, requires you to have read them all and assumes that you already know most everything about mail and message movement within a system.

Also, I found several articles suggesting to use a transport file with a very similar layout as the bysender file here, but that just caused the mail server and MailRoute to play hot potato with the message (tossing it back and forth). From what I figured is the transport file is used to override DNS entries to direct mail for a particular domain to another server that may not exist in DNS records or is behind a firewall.

I hope that this was informative and helpful to your needs.


Resources