How to configure Postfix to only send out emails with a certain header?

I'm setting up a local development VM and I was able to redirect all sent out emails to a local mailbox using virtual_alias_maps with the entry /.*/ username.

What I'd like to add is an option to not redirect emails to the local mailbox if the email contains a certain header.

Or in other words; if a certain header is found in the outgoing email, send it, but if the header is missing, it should be redirected to the local mailbox.

How would I be able to achieve this using Postfix?

Thanks a lot for your help!


Solution 1:

First we define two smtpd processes in master.cf, the one with rewrite enabled and the other with rewrite disabled. SMTPD with rewrite enabled was exist by default in master.cf. Now we will these lines

127.0.0.1:2525 inet n  -       n         -       2000     smtpd
    -o receive_override_options=no_header_body_checks,no_address_mappings

This will enable another smtpd process

  • Listen in port localhost IP port 2525
  • With header_checks feature and rewrite feature disabled.

Here the pseudocode of your request

if header exist (postfix will checks via header_cheks)
    no rewrite 
else
    rewrite with virtual_alias_maps

We will implement that logic With header_checks.

Add this line in main.cf

 header_checks = regexp:/etc/postfix/mycustomheader

Content of /etc/postfix/mycustomheader

/^X-Your-Header/  FILTER smtp:[127.0.0.1]:2525

How it works

Whenever postfix detects X-Your-Header in your messages, it will redirect email to second smtpd process as we defined earlier. If the header not exist, your email will going through default smtpd process with rewrite enabled.

Solution 2:

Scapping pseudocode from tpml7

if header exist (postfix will checks via header_cheks)
    no rewrite --> different instance
else
    rewrite with virtual_alias_maps same instance

AFAIK postfix can redirect email if certain header exist. See REDIRECT feature at man header_checks.

Unfortunately - based on above pseudocode - your request is redirecting email if certain header doesn't exist. I already tried some simple workarounds. But because your request wasn't directly supported then this solution was little complicated. Especially this solution required to learning about postfix multi instance.

I'll write the outline here - not exact step by step -, feel free to ask if something was still vague.


To do that you need two multi instances. First instance is doing header_checks, Second instance is doing normal outgoing.

First instance will have two smtpd daemon defined in master.cf. One daemon (default) listen at *:25, the second one listen at 127.0.0.1:12525.

Second instance will listen at 127.0.0.1:22525

First instance

##main.cf

# your header filter
header_checks = pcre:/path/to/header_checks

# no rewriting
receive_override_options = no_address_mappings

# send to second instance
relayhost = [127.0.0.1]:22525
virtual_alias_maps = pcre:/your/catch-all

##master.cf

# second smtpd but with rewriting enabled
[127.0.0.1]:12515 inet  n       -       n       -       -       smtpd
    -o receive_override_options=

# /path/to/header_checks
/^MyHeader/  FILTER smtp:[127.0.0.1]:12525

Second instance

Just setup like first postfix instance without header_checks, relayhost, virtual_alias_maps and receive_override_options (normal postfix instaltion). Set master.cf so it's only listen at [127.0.0.1]:22525. Set myhostname so it will be different with first instance.