Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer
    
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 '11 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. –  Cucumber Jun 28 '11 at 18:08
    
Is it possible to redirect to more than one email-address and to deliver to the original recipient? –  harp Oct 20 at 13:13

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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