6

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

When an e-mail is:

  • Sent from: user@isp.com
  • Addressesd to: user@ourcompany.com

Result: redirect e-mail to user2@ourcompany.com.

I don't want to catch *@isp.com and redirect, and I don't want to redirect all e-mail addressed to user@ourcompany.com but only redirect when user@isp.com sends user@ourcompany.com 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

0

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

header_checks = pcre:/etc/postfix/headers_check

/etc/postfix/headers_check:

/To:.*@(?!mail.domain.com) && From:.*@?!extdomain.com/ REDIRECT mailbox@mail.other.domain.com

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

5
  • 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
    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. 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
    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
    Sep 14, 2015 at 5:45
  • This answer makes stuff up. Why has it been accepted? Has anyone tested this? Jun 24, 2020 at 9:03
3

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

/etc/postfix/main.cf:

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...]

/etc/postfix/maps/user_to_redirect:

user@isp.com                   redirect

/etc/postfix/maps/redirections:

user@ourcompany.com            user2@ourcompany.com

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

1

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.

0

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:

main.cf

header_checks = regexp:/etc/postfix/header_checks

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

http://mattshaw.org/news/how-to-filter-mail-with-postfix-header_checks/

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

From: Mr Smith <smith@server.com>

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:

sender@otherdomain.com REDIRECT you@yourdomain.com

Just remember to do postmap hash:sender_access after saving sender_access

0

This is a working config for forwarding all mail from extdomain1.ltd to special box specialuser@domain.ltd:

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

#cat /etc/postfix/header_checks
/From:.*@extdomain1.ltd/ REDIRECT specialuser@domain.ltd

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

-1

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

sender@otherdomain.com REDIRECT you@yourdomain.com

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

sender@otherdomain.com REDIRECT you@yourdomain.com | your2@secondomain.com

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.