Postfix relay to multiple servers and multiple users

postfix

I currently have postfix configured so that all users get relayed by the local machine with the exception of one user that gets relayed via gmail. To that extent I've added the following configuration:

/etc/postfix/main.cf

# default options to allow relay via gmail
smtp_use_tls=yes
smtp_sasl_auth_enable = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_sasl_security_options = noanonymous

# map the relayhosts according to user 
sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_maps

# keep a list of user and passwords
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

/etc/postfix/relayhost_maps

user-one@localhost   [smtp.gmail.com]:587

/etc/postfix/sasl_passwd

[smtp.gmail.com]:587  user-one-at-google@gmail.com:user-one-pass-at-google

I know I can map multiple users to multiple passwords using smtp_sasl_password_maps but that would mean that all relay would be done by gmail where I specifically want all relay to be done by the localhost with the exception of some users.

Now I would like to have a user-two@localhost (etc) relay via google with their own respective passwords. Is that possible?

Best Answer

Set parameter smtp_sender_dependent_authentication to yes. Then you can user sender address as lookup key for smtp_sasl_password_maps.

References: official documentation and this page

In practical terms what you need to do is simply change the key in your sasl_passwd to user based. As per your example just add both users to the relayhost_maps file and alter the following file:

/etc/postfix/sasl_passwd

user-one@localhost       user-one-at-google@gmail.com:user-one-pass-at-google
user-two@localhost       user-two-at-google@gmail.com:user-two-pass-at-google