Saturday, September 15, 2012

Redirect all outbound emails to one address

Short Version:

sudo apt-get install postfix

sudo vi /etc/postfix/main.cf

    # add into /etc/postfix/main.cf
    always_bcc = youremail@yourdomain.com
    transport_maps = hash:/etc/postfix/transport

sudo vi /etc/postfix/transport

    # add into /etc/postfix/transport
    your-email@your-domain.com :
    * discard:

sudo postmap /etc/postfix/transport
sudo /etc/init.d/postfix restart


Long Version:
 
Took me hours to find this "clean" method... Just to save you some time from searching...

You might need this because...

On my local development server, I don't want to send emails to users in my dev database  which in my case is a copy of the production db. I need catch all out going emails and redirect them to my email address so I can test if the code sends correct emails without spamming real users.

According to the second reference link at the bottom, the way I used here does what you want without filling your outbound queue or requiring CPU time to process all the bounces. 

What to do:

# Install Postifx:

sudo apt-get install postfix

# Edit postfix configuration file

sudo vi /etc/postfix/main.cf

# add following settings

# bcc all outbound (and inbound?) email to developer
# why bcc? you can still see "To" in emails, useful for testing

always_bcc = youremail@yourdomain.com

# redirect emails based on /etc/postfix/transport
transport_maps = hash:/etc/postfix/transport

# optional, just in case some mail server reject emails from development server, change myhostname to a FQDN
myhostname = your-domain.com

# create mapping

sudo vi /etc/postfix/transport

# add following lines

# no restriction for your email your-email@your-domain.com
# as you still need BCC going out to your email 
# format below: email [space] [colon] [new line]

your-email@your-domain.com :

# all other emails going to nowhere/black hole
# [asterisk] [space] discard[colon]

* discard:

# apply map

sudo postmap /etc/postfix/transport

# restart postfix

sudo /etc/init.d/postfix restart

# reference
# http://www.informedemail.com/blog/2011/01/using-postfix-transport-maps-for-email-address-specific-aliasing.html
# http://rene.bz/redirect-all-outgoing-email-single-account-postfix/

No comments:

Post a Comment