Conditional use of relay(smarthost) for recipient domains matched by header_checks in postfix

emailpostfixsmarthost

On a postfix mailserver, which is configured to generally send mail directly (internet-site), I want to implement a conditional header check for some receipient domains (in this example to keep it simple just with gmail.com), to let postfix use a smarthost.
But, instead of authenticating with the smarthost smtp.google.com, it just keeps sending the mails addressed To: *@gmail* directly (if I set a wrong password on purpose, it does not complain about).

I created the following files in /etc/postfix/:

relay_passwd:

smtp.google.com mygmailaccount@gmail.com:mypassword

header_checks:

^To: <*@gmail*>/ FILTER relay:smtp.gmail.com:25

and referenced them in main.cf with additional options:

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/relay_passwd
header_checks = regexp:/etc/postfix/header_checks

now I postmapped the files (and since, restarted postfix too multiple times):

# postmap relay_passwd
# postmap header_checks

and it throws a warning:

postmap: warning: header_checks, line 1: record is in "key: value" format; is this an alias file?

But I guess that's not the problem, is it? Anyway, I must be doing something wrong.

I found the following threads but they're not quite what I want:

Use different relay in postfix

Conditionnal relay in postfix

Different postfix relayhost based on system user?

Is the order in main.cf relevant? Is the regexp in header_checks ok?

Best Answer

This should do it, your relay configuration looks fine.

/^To: (.*?)@gmail.com$/ FILTER smtp:smtp.gmail.com

Also, you don't need to postmap regexp files.

Source: Marcelog