Postfix: Modify sender address based on recipient

postfixsmtp

We have a Postfix server that receives mail from our application servers. Senders are in the form user@host.fqdn (where host.fqdn can vary, depending on source server) and recipients can be internal or external users.

Messages going to external users should have the sender changed to user@domain.com. I have tried using canonical maps, but since that is handled by the cleanup daemon, before any transport decisions are made, it would affect all sender addresses.

I have also tried creating a custom smtp transport with generic mappings and configuring transport_maps to use that custom smtp transport for external domains. However, generic mappings affect both sender and recipient addresses.

Lastly, I've tried the following:

  • Create a custom smtpd daemon that specifies sender canonical maps and a unique transport table.
  • Send all externally addressed mail to that custom daemon. Ideally, sender canonical maps would transform the sender address and the unique transport table would relay messages to the internet.

However, evidently, only one transport table can be used per Postfix instance.

I want to avoid creating an entirely new Postfix instance to accommodate this rewriting. Any suggestions? (and thanks in advance)

Best Answer

Disclaimer: this answer won't work as intended when process email which had multiple recipients with both internal and external domain in it

With postfix multi instance, the solution would be trivial. Just setup transport_maps to second instance, then do rewriting in second instance.

Without multiple instance, you need two smptd processes, two cleanup processes and access map.The idea is do filtering in access maps instead in transport_maps. When an email has external domain recipient, it transported to second smtpd daemon. Then second smtpd daemon would call second cleanup daemon. First cleanup daemon won't have canonical maps parameter, while in second cleanup daemon, we will add that parameter to do rewriting.

In main.cf, define check_recipient_access in smtpd_*_restriction. For example:

smtpd_recipient_restriction =
   ... 
   check_recipient_access hash:/etc/postfix/external-filter
   ... 

In external-filter file define a filtering mechanism

external.example.com    FILTER smtp:[127.0.0.1]:12525

In this example we will setup second smtpd daemon listen in 127.0.0.1 port 12525.

Now, setup second smtpd and cleanup daemon in master.cf

[127.0.0.1]:12525  ....  smtpd -o cleanup_service_name=cleanup_rewrite

cleanup_rewrite .... cleanup -o canonical_maps=hash:/etc/postfix/mysender_rewriting

File mysender_rewriting would contains a sender mapping as you intended.

Related Topic