Postfix change to address based on sending host

emailhostnamepostfix

We have one central postfix mailserver and some more application servers behind whereas those servers relaying the mails trough the central mailserver. now i want to configure the central one to rewrite the TO-Field from the original recipient to a generic one like testing@example.com but ONLY from 2 specific application servers, so:

postfix has to check from which applicationserver the mail is coming (either by IP or better by hostnames (FQDN) and if the hostname matches app1.example.com or app2.example.com, then the mail recipient is always testing@example.com.

if the hostnames is not app1 or app2 then the original recipient remains (no rewrite).

i only managed to rewrite all mails TO header but not by sending hostname/ip

Best Answer

A not so elegant solution would be to use REDIRECT in header checks. It will send your email to desired address but it will not rewrite the TO header.

In main.cf uncomment or put

header_checks = regexp:/etc/postfix/header_checks

if it is not already.

In /etc/postfix/header_checks put something like:

/^Received:.*app1.example.com.*/ REDIRECT testing@example.com

If reverse DNS does not work correctly you better use the IP instead.

Don't forget to run postmap /etc/postfix/header_checks