# Postfix: how to redirect a single email address depending on the sender

I'm trying to add a rule to Postfix.

My goal is as follows: when anyone EXCEPT (server@example.com) sends an email to (info@example.com) forward it to (sales@example.com). If server@example.com is sending the mail, simply allow it to flow through. To recap, if the sender is NOT server@example.com, redirect the message to sales@example.com. If the sender IS server@example.com, do not redirect.

FYI, info@example.com is an alias, defined in /etc/postfix/virtual which sends to several recipients.

I am using pcre header_checks, but it's simply not working - nothing is happening and all mail is flowing as it was before without being redirected.

If there is a better/easier way to do this, I'm all ears!

So per the instructions in this post and this post I added the following lines to main.cf and headers_check:

main.cf:

header_checks = pcre:/etc/postfix/headers_check


/To:info@example.com/ && /From:(?(?!server@example.com).)*/ REDIRECT sales@example.com


However, nothing happens. All mail still gets dropped into info@example.com. I removed the 'From' check to see if I could just grab all messages sent to info@example.com and redirect. However, this didn't work either. I ran

postmap -q "From:server@example.com\nTo:info@example.com" pcre:/etc/postfix/headers_check


&& /(?(?!server@example.com).)*/ REDIRECT sales@example.com


Also, please note that this is a machine having lots of virtual hosts, each with its own email addresses and aliases. All are combined in /etc/postfix/virtual.

Thanks for any advice on how to accomplish this!

I don't know why this answer was accepted and upvoted.Postfix documentation clearly states that postfix only examines one header line at a time.

These rules operate on one logical message header or one body line at a time. A decision made for one line is not carried over to the next line.

So, your idea to combine From and To header isn't supported by Postfix.

The other way is using envelope address instead address in email header. To do this you can use postfix feature called restriction classes. See

smtpd_restriction_classes = specialsender
specialsender = permit
smtpd_recipient_restrictions =
...
check_sender_access hash:/etc/postfix/mycompany.redirection,
check_recipient_access  hash:/etc/postfix/redirect.sales
...


Content of /etc/postfix/mycompany.redirection

server@example.com     specialsender


Content of /etc/postfix/redirect.sales

info@example.com      REDIRECT sales@example.com


### How it works

When postfix encounter this line

check_sender_access hash:/etc/postfix/mycompany.redirection


and found out that server@example.com in specialsender class, then postfix permit it without redirect. Another email address will be checked against

check_recipient_access  /etc/postfix/redirect.sales


And if recipient == info@example.com, it will be redirected to sales@example.com