Rewrite all email addresses using regex in postfix sender canonical file

To rewrite all email addresses to a specific address like "info@magesystem.net" in the Postfix sender_canonical file using regular expressions, you can follow these steps:

  1. Edit the sender_canonical File:

    Open the sender canonical mapping file. This file is usually located at /etc/postfix/sender_canonical. You can edit it using a text editor with superuser privileges:

    sudo nano /etc/postfix/sender_canonical
    
  2. Add Regular Expression Mapping:

    In the sender_canonical file, add regular expression mappings to rewrite email addresses. For example, to rewrite all email addresses to "info@magesystem.net," you can use the following format:

    /^(.*)$/ info@magesystem.net
    

    This regular expression ^(.*)$ captures any email address and rewrites it to "info@magesystem.net."

  3. Update Postfix Configuration:

    Open your Postfix main configuration file, typically located at /etc/postfix/main.cf, using a text editor:

    sudo nano /etc/postfix/main.cf
    
  4. Configure sender_canonical_maps:

    Add or modify the sender_canonical_maps parameter in your main.cf file to specify the path to your sender_canonical file:

    sender_canonical_maps = regexp:/etc/postfix/sender_canonical
    

    This configuration tells Postfix to use the sender_canonical file with regular expressions for address rewriting.

  5. Reload Postfix Configuration:

    After making changes to the main.cf file, you should reload the Postfix configuration to apply the changes:

    sudo systemctl reload postfix
    

Now, Postfix will rewrite all email addresses using the regular expression specified in the sender_canonical file to "info@magesystem.net" when sending outbound emails. Make sure your regular expression in the sender_canonical file is crafted carefully to match the email addresses you want to rewrite accurately.

 

sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps =  regexp:/etc/postfix/sender_canonical_maps
smtp_header_checks = regexp:/etc/postfix/header_check

Rewrite envelope address from email originating from the server itself

/etc/postfix/sender_canonical_maps:

/.+/    newsender@address.com

Rewrite from address in SMTP relayed e-mail

/etc/postfix/header_check:

/From:.*/ REPLACE From: newsender@address.com