Sunday, May 13, 2012

Redirect all a server's email on Ubuntu

I brought a new application server online and needed to test the batch email for several weeks.  For obvious reasons I didn't want the actual emails to go out and I didn't want to modify the application to fake the addresses.  The best solution was to modify the postfix configuration to catch and redirect all outbound messages.  My configuration is Ubuntu 10.04.  

I scoured the web for a solution and found many incomplete or overly complicated solution.   The credit for the best one goes to Mattias Ahnberg on ServerFault.com.  Here are his exact instructions:

Create /etc/postfix/virtual-regexp with the following content:
/.+@.+/ email@gmail.com
Edit /etc/postfix/main.cf and add regexp:/etc/postfix/virtual-regexp to the virtual_maps configuration. The end result might look like this in main.cf:
virtual_maps = hash:/etc/postfix/virtual, regexp:/etc/postfix/virtual-regexp
Build the mapfile by typing:
postmap /etc/postfix/virtual-regexp
This also requires a virtual.db to exist. If it doesn't create an empty file called virtual and run : postmap /etc/postfix/virtual

This worked like a charm and the first time.   To disable I just commented virtual_maps line in the main.cf.  Thanks Mattias!  The original post.