Conditionally rewriting From and Reply-To headers in Postfix

amazon ec2amazon-sesamazon-web-servicespostfix

I've got a web app running on Amazon AWS and it's using Amazon SES to send emails. One of the things I really want this system to do is to be able to forward emails to certain addresses from anyone who mails it, for example if some random person emails contact@mysite.com then I want it to forward to my personal email address.

I tried to do this with aliases, but it doesn't work due to restrictions in Amazon SES which disallow me from sending mail from un-verified senders. It works fine if I email contact@mysite.com from an email address that is verified, but not if I email from an unverified address.

So, what I'd like to try to do is rewrite the From and Reply-To headers on any messages that are sent to contact@mysite.com (and also on other addresses, such as support@mysite.com). I'd like to move the original From header to the Reply-To header, and then change the From header to no_reply@mysite.com (or something like this). For example:

From: "Bruce Wayne" <bruce@batman.com>
To: "MySite Support" <support@mysite.com>

Would become

From: "Mail Services" <no_reply@mysite.com>
Reply-To: "Bruce Wayne" <bruce@batman.com>
To: "MySite Support" <support@mysite.com>

I have support@mysite.com defined in the aliases file, and it transforms it into my personal email. I'm not sure how the aliases stuff will be affected by header_checks or whatever is needed to solve this.

There is also a catchall alias which sends other emails into my webapp, so this whole header-rewriting stuff is really something that I only need/want for very specific addresses as described above.

Rewriting a single header using regexp seems simple enough, but the things I'm unsure about here are

  1. conditionally rewriting based upon the address of the To header and
  2. rewriting the Reply-To header based upon the value of the From header.

Can anyone give me some pointers on how to deal with these two problems? Or if I'm looking at this problem the wrong way, any direction would be much appreciated.

UPDATE – I somehow missed that there are if/endif statements in header_checks. I'm trying to do something like this, but my header checks don't appear to be doing anything:

if /^To: (support@mysite.com|contact@mysite.com)/
/^From:(.+@.+).*$/ PREPEND Reply-To:$1
/^From:(.+@.+).*$/ REPLACE From: no_reply@mysite.com
endif

I've added the following line to main.cf:

header_checks = pcre:/etc/postfix/header_checks

It's unclear whether I'm supposed to postmap this file. I tried both doing it and not doing it and it made no difference.

Is there something obvious that I'm doing wrong here?

Best Answer

The IF pattern matches the same line as other patterns between IF and ENDIF do, so you cannot mix To: and From: headers in the statement. See BUGS in header_checks(5) man page.