using postfix for secure tests with live data

During web development and quality assurance you sometimes want to test an application with live data (e.g. a web shop test instance with live customer sql data). As your application implements mail functionality you have to make sure that no mail on the test server is sent to a real user but you probably want to be able to send mails to developers and receive all other outgoing test mails in a catch-all account. An easy Postfix tweak for this is using a pcre regular expression virtual map.

So our goal is: All outgoing mails to a specific domain should be delivered normally (e.g. your company domain) while all other outgoing mails should be delivered to one specific catch-all mailbox.

Actually this task can be resolved by adding not more than three lines of configuration code in two files.

1. in /etc/postfix/ add

virtual_alias_maps = pcre:/etc/postfix/virtual.pcre

2. the new /etc/postfix/virtual.pcre should look like

/(.+)@yourowndomain\.tld$/ ${1}@yourowndomain.tld
/(.+)@(.+)/ catchoutgoing@yourowndomain.tld

After having restarted postfix you are already done.

Please note, a ${0} instead of ${1}@yourowndomain.tld did not work for me, so this a tiny workaround which does not look beautiful but really does it’s job.

p.s.: A really nice (but somehow old looking) tool for developing and testing regular expressions is „redet„, a „regular expression development and execution tool“ which knows nearly every important regex syntax depending on the tools you have installed. You’ll find redet in your Debian/Ubuntu repository.