Linux – Delivering mail for some users to external SMTP while the domain is local on Postfix

centos7linuxpostfix

I want to send mail to an outside Office 365 account with the same domain I have for local accounts.

When I try to send mail to this address outside, Postfix rejects it with error "Account not available".
So, I want Postfix to send mail to outside account for same domain, if that account is not present locally.

For example I have two email address abc@example.com and xyz@example.com.

  • abc@example.com is present in local server
  • xyz@example.com is an account on Office 365 external mailserver.

Now, I want to send mail to xyz@example.com from Postfix from any local account. The domain example.com is added to relay_domains also, but Postfix still rejects mail with error "recipient not present".

Best Answer

As you already have example.com as a local domain configured in either mydestination or virtual_alias_domains you can't achieve this by also adding it to relay_domains.

Instead, you should add a transport(5) map that can override delivery rules defined by the default transport:nexthop used by mydestination, virtual_alias_domains etc.

DESCRIPTION

The optional transport(5) table specifies a mapping from email addresses to message delivery transports and next-hop destinations. Message delivery transports such as local or smtp are defined in the master.cf file, and next-hop destinations are typically hosts or domain names. The table is searched by the trivial-rewrite(8) daemon.

This mapping overrides the default transport:nexthop selection that is built into Postfix.

Add transport_maps to your /etc/postfix/main.cf:

transport_maps = hash:/etc/postfix/transport

And then add to /etc/postfix/transport:

xyz@example.com    smtp:example-com.mail.protection.outlook.com
@example.com       :
*                  :

(Remember to postmap /etc/postfix/transport as you are using hash, Berkeley DB lookup table.)

This will use default transport rules for all @example.com and anything else (*) leaving other delivery configuration as is, but forward all email to xyz@example.com to Office 365 via SMTP: the default pattern for Office 365 MX record for companies is <domain>.mail.protection.outlook.com.


Using transport_maps is a global solution that will work with any external SMTP server. In a specific scenario, where a) the external account is on Office 365 and b) domain is configured on Postfix virtual_alias_domains it could also be possible to add a virtual alias redirecting all mail to Office 365 user's initial domain (tenant) address, xyz@example.com xyz@contoso.onmicrosoft.com.

Related Topic