I did something like this using transport_maps and it still doesn't work .
/etc/postfix/Main . cf
mynetworks = 127 . 0 . 0 . 0/8, [ : : ffff : 127 . 0 . 0 . 0]/104, [ : : 1]/128, 192 . 168 . 1 . 0/24,
mydestination = $myhostname, smtprelay, localhost . localdomain, , localhost, localhost . o365 . local, localhost . gmail . local
# default relayhost setting
relayhost = [smtp . gmail . com] : 587
# sender-dependent sasl authentication
smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash : /etc/postfix/sender_relay
# smtp authentication settings
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash : /etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates . crt
smtp_sasl_mechanism_filter = plain
transport_maps = hash : /etc/postfix/transport
--------------------------------------------------------------------------------------------------------------------------
/etc/postfix/sasl_passwd
gmail@gmail . local account1@gmail . com : Start321!
o365@o365 . local account1@mydomain . com : Start321!
------------------------------------------------------------------------------------------------------------------------
/etc/postfix/sender_relay
account1@gmail . com [smtp . gmail . com] : 587
account1@mydomain . com [smtp . office365 . com] : 587
----------------------------------------------------------------------------------------------------------------------
/etc/postfix/transport
gmail . local smtp : [smtp . gmail . com] : 587
o365 . local smtp : [smtp . office365 . com] : 587
At this time, only sending from gmail@gmail . local works and for O365 there is such an error :
321D0100A34 : from=<o365@o365 . local>, size=422, nrcpt=1 (queue active)
Nov 14 10 : 30 : 51 smtprelay postfix/smtp[6261] : 321D0100A34 : SASL authentication failed; server smtp . gmail . com[173 . 194 . 73 . 108] said : 535-5 . 7 . 8 Username and Password not accepted . Learn more at?535 5 . 7 . 8 https : //support . google . com/mail/?p=BadCredentials i22sm2015187ljg . 94 - gsmtp
Nov 14 10 : 30 : 51 smtprelay postfix/smtp[6261] : connect to smtp . gmail . com[2a00 : 1450 : 4010 : c0d : : 6d] : 587 : Network is unreachable
Nov 14 10 : 30 : 51 smtprelay postfix/smtp[6261] : 321D0100A34 : to=<myemail@mydomain . com>, relay=none, delay=0 . 43, delays=0 . 07/0/0 . 36/0, dsn=4 . 4 . 1, status=deferred (connect to smtp . gmail . com[2a00 : 1450 : 4010 : c0d : : 6d] : 587 : Network is unreachable)
After hashing the entry #relayhost = [smtp . gmail . com] : 587 in main . cf, nothing works and every message wants to come out via O365 but he fails .
Nov 14 10 : 16 : 08 smtprelay postfix/qmgr[5995] : 31D7F100A34 : from=<o365@o365 . local>, size=422, nrcpt=1 (queue active)
Nov 14 10 : 16 : 38 smtprelay postfix/smtp[6013] : connect to xxx . mail . protection . outlook . com[104 . 47 . 1 . 36] : 25 : Connection timed out
Nov 14 10 : 15 : 00 smtprelay postfix/qmgr[5995] : 560EF100A34 : from=<gmail@gmail . local>, size=437, nrcpt=1 (queue active)
Nov 14 10 : 15 : 30 smtprelay postfix/smtp[6013] : connect to xxx . mail . protection . outlook . com[104 . 47 . 0 . 36] : 25 : Connection timed out
In the first case ipostfix trying to authorize gmail via o365 account. But you will notice the message is sent from
o365@o365.local and should be redirected to the server smtp.office365.com and not to smtp.gmail.com. So it doesn't look at the settings in the transport file at all.
In the second case, it is strange because it does not matter if I send from the address gmail.com or o365.local it is always redirected to the address smtp.office365.com, which means that it does not pay attention to the settings in the trasport file and tries to connect on port 25 which is blocked for this service we get a time out message. As you can see in the configuration files, port 587 is everywhere.