I need to redirect e-mail within our MTA when the two following criteria are both true:

When an e-mail is:

Result: redirect e-mail to [email protected].

I don't want to catch *@isp.com and redirect, and I don't want to redirect all e-mail addressed to [email protected] but only redirect when [email protected] sends [email protected] an e-mail.

How do I achieve this within Postfix's configuration. And if it's not possible within Postfix, what may be the best solution?

6 Answers 6


You can use PCRE In /etc/postfix/main.cf:

header_checks = pcre:/etc/postfix/headers_check


/To:.*@(?!mail.domain.com) && From:.*@?!extdomain.com/ REDIRECT [email protected]

PCRE works with perl regular extentions, and you can specify any conditions.

  • Sure the "&&" operator does work? Found nothing in the header_checks manpage and everything not documented in Postfix doesn't exist. I'm afraid I cannot falsify this with a test at the moment.
    – sebokopter
    Commented Jun 28, 2011 at 15:27
  • Likely, correctly will be as follows: 'm/To:.*@(?!mail.domain.com)/ && m/From:.*@(?!extdomain.com)/' But I can't verify this on realy work server. And you have to replace "To:.*@(?!mail.domain.com)" to you normal address. Commented Jun 28, 2011 at 18:08
  • Is it possible to redirect to more than one email-address and to deliver to the original recipient?
    – harp
    Commented Oct 20, 2014 at 13:13
  • 4
    The voted answer will not work because postfix checks one header at a time, as the manual clearly states. postfix.org/header_checks.5.html
    – bserem
    Commented Sep 14, 2015 at 5:45
  • This answer makes stuff up. Why has it been accepted? Has anyone tested this? Commented Jun 24, 2020 at 9:03

If you don't want to use procmail then maybe you want something like the following:


smtpd_restriction_classes = redirect
redirect = check_recipient_access hash:/etc/postfix/maps/redirections
smtpd_recipient_restrictions = [...some checks...],
                               check_sender_access hash:/etc/postfix/maps/user_to_redirect,
                               [...some more checks...]


[email protected]                   redirect


[email protected]            [email protected]

For more information see: http://www.postfix.org/RESTRICTION_CLASS_README.html


have you considered a .forward with procmail? Check here and let us know if this answers your need.

Specifically, combining the "Forwarding" instructions, for pushing mail back out to another address, with the "Spam" section, for identifying the correct emails, would seem to do exactly what you're looking for.


My postfix didn't have pcre installed:

fatal: dict_open: unsupported dictionary type: pcre:  Is the postfix-pcre package installed?

But the basic regex ones worked, as outlined here:


header_checks = regexp:/etc/postfix/header_checks

The syntax in the header_checks file is:
/regex_pattern/ ACTION


Be aware too that the users name comes before the email address in the header line starting with From, eg:

From: Mr Smith <[email protected]>

So your regex will need to take that into account.

But I found a simpler solution that doesn't require regexes as long as you don't mind not being able to control the rule based on the recipient.

In modern versions of postfix you can add a redirect command to sender_access

Assuming you have a line in your main.cf like so:

smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender_access

You can add a line to sender_access to redirect the email to another address like so:

[email protected] REDIRECT [email protected]

Just remember to do postmap hash:sender_access after saving sender_access


This is a working config for forwarding all mail from extdomain1.ltd to special box [email protected]:

#cat main.cf
header_checks = pcre:/etc/postfix/header_checks

#cat /etc/postfix/header_checks
/From:.*@extdomain1.ltd/ REDIRECT [email protected]

Don't forget to send update postfix command to re-read configs.


The problem with it all is that it can only redirect to one single email address with either of the methods at all.

[email protected] REDIRECT [email protected]

for example the following will not work already as it's pretty much limited in Postfix:

[email protected] REDIRECT [email protected] | [email protected]

If your are to do some more fancy stuff I'd strongly suggest that your install and learn how to use procmail.

You must log in to answer this question.

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