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

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:

[email protected]    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 [email protected] 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, [email protected] [email protected].