Why would you want to only send all outgoing mail to a single email address ?

Imagine that you have been given a web application from a client. You set it up to run in your local LAMP (or MAMP) stack and start poking the application to try and get your head around it so you can try and make the changes that your client or boss have asked you to make.

What you didn’t realise is that they gave you the production database and that when you have been poking around in the system it has been firing off work flow emails to the customers telling them that their accounts have been locked or deleted or anything else that the system.

How to set postfix to only send to a single email address

Assuming you are using postfix, and you want to get a copy of all your emails at a specific email address (for example [email protected]),

Make the contents of your /etc/postfix/transports file

[email protected] :
* discard:

Which tells postfix, for emails to [email protected], use the default delivery mechanism (most likely SMTP), but for anything else use the discard delivery mechanism (i.e. drop the email). We could have specified multiple email addresses here, or even a domain instead of [email protected].

Add to your /etc/postfix/main.cf file

transport_maps = hash:/etc/postfix/transports
always_bcc = [email protected]

Then run the command

sudo postmap /etc/postfix/transports

Which should create the /etc/postfix/transports.db file.

Test it by running

echo test | mail -s test [email protected]

Then check your logs for

Oct 26 15:35:50 localhost postfix/pickup[13308]: 568E062147F: uid=1000 from=<localhost>
Oct 26 15:35:50 localhost postfix/cleanup[14006]: 568E062147F: message-id=<20111026043550.568E062147F@localhost>
Oct 26 15:35:50 localhost postfix/qmgr[1929]: 568E062147F: from=<localuser@localhost>, size=335, nrcpt=2 (queue active)
Oct 26 15:35:50 localhost postfix/discard[14011]: 568E062147F: to=<[email protected]>, relay=none, delay=0, delays=0/0/0/0, dsn=2.0.0, status=sent (example.com)
Oct 26 15:35:50 localhost postfix/smtp[14012]: 568E062147F: to=<[email protected]>, relay=127.2.2.2[127.2.2.2]:25, delay=0.1, delays=0/0/0/0.09, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 7E20116AC00B)
Oct 26 15:35:50 localhost postfix/qmgr[1929]: 568E062147F: removed

The important parts here are that delivery to the actual address is being processed by postfix/discard and that delivery to the bcc address is being processed by postifx/smtp which you can see in the log fragment above on lines 4 and 5 just after the date and and hostname (localhost).