5

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!

4
  • Is this question about programming (extending postfix), or configuring ?
    – KeatsPeeks
    Feb 19, 2014 at 20:20
  • Configuring I guess.
    – thasmo
    Feb 19, 2014 at 20:57
  • What do you mean by "it should be left alone"? Should that mail be delivered to the target system according to the MX entries of the target mail address? Or should it be discarded? Or something else? Can you post your main.cf?
    – mgabriel
    Dec 31, 2014 at 19:25
  • Yep. If there's a specific header present in the email, it should be sent and delivered, otherwise (if the header is not present) the email should be redirected to a local mailbox only.
    – thasmo
    Dec 31, 2014 at 20:55

2 Answers 2

2
+100

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.

2
  • Ah... yes. My answer was flawed because rewriting happened before header_checks. As a result, when transported to second smtpd the recipient was already changed to catch-all recipient :(
    – tpml7
    Jan 7, 2015 at 8:04
  • I'll award the bounty to your answer as this one was improvement of my answer.I'll let the OP verify if this is working solution for him.
    – tpml7
    Jan 7, 2015 at 8:29
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.

1
  • 2
    Thanks for your help! Seems like the mails are still received locally only. Looks like the mail gets forwarded to the smtpd process on port 2525, but instead of sending out the mail it gets redirected to the local mailbox again. I've checked everything for typos and so far it all looks good to me. Any idea why this still might fail? Thanks!
    – thasmo
    Dec 30, 2014 at 0:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .