So if someone stumbles over this like I did: the answer is indeed header_checks and it works as such:
Add the following line to /etc/postfix/main.cf
:
header_checks = regexp:/etc/postfix/header_checks
Add the new file /etc/postfix/header_checks
with this content:
/^To:.*@allowed-domain.com/ DUNNO
/^To:.*@/ REDIRECT redirect@example.com
Replace allowed-domain.com
with the domain your mailserver should still send mails to. Replace redirect@example.com
with the email address all other emails should be redirected to.
If you need to allow multiple domains, the first line should look like this:
/^To:.*@(allowed-domain.com|another-domain.com)/ DUNNO
Instead of redirecting you can simple drop all other mails. Replace the second line above with:
/^To:.*@/ DISCARD No outgoing mails allowed
Explanation:
- Postfix goes through the mail headers one-by-one.
- Each header line gets matched against the
header_checks
file line-by-line.
- If it matches the first line (
To:
contains the allowed domain), it skips to the next header line and starts the header checks again from the top. Since no other line will match, this means the mail gets delivered.
- If it matches the second line (
To:
contains another external email address), it redirects the mail.