Conditionally rewriting From and Reply-To headers in Postfix

9,351

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.

Share:
9,351

Related videos on Youtube

bratsche
Author by

bratsche

I work at Microsoft. I do stuff with GTK+, Xamarin, Mono, Elixir, and mobile. I play viola. https://github.com/bratsche

Updated on September 18, 2022

Comments

  • bratsche
    bratsche over 1 year

    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 [email protected] 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 [email protected] 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 [email protected] (and also on other addresses, such as [email protected]). I'd like to move the original From header to the Reply-To header, and then change the From header to [email protected] (or something like this). For example:

    From: "Bruce Wayne" <[email protected]>
    To: "MySite Support" <[email protected]>
    

    Would become

    From: "Mail Services" <[email protected]>
    Reply-To: "Bruce Wayne" <[email protected]>
    To: "MySite Support" <[email protected]>
    

    I have [email protected] 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: ([email protected]|[email protected])/
    /^From:(.+@.+).*$/ PREPEND Reply-To:$1
    /^From:(.+@.+).*$/ REPLACE From: [email protected]
    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?