Redirecting outgoing mail with Postfix

We have a various staging deployments of our systems at Brightbox and need to test that the emails they send are correct. We have a bunch of test accounts registered with various email addresses and we wanted them all to go to our dev team, rather than the original recipient.

Rather than write support for this into our apps, we used Postfix to redirect the mail to our devs.

In our case, our staging deployments use a local installation of Postfix and the systems are generally not used by anything else, which makes this dead easy.

Firstly, write a rewrite map file, with the following one line of content. Call it /etc/postfix/recipient_canonical_map:

/./ devteam@example.com 

Then configure Postfix like this (in /etc/postfix/main.cf):

 recipient_canonical_classes = envelope_recipient recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map 

Now all mail going through this relay will be redirected to devteam@example.com. It rewrites only the envelope, so the important headers are not changed.

Comments

Jan says:

Hi,

thx. Ive got the same scenario, and i was happy to get this done in less than 5 min.

Thank you very much

Daan Biere says:

Hi John,
Great solution, implemented within 5 minutes :)
One step further in having a controlled test/acceptance environment.

very useful

Kind Regards,
Daan Biere

ilyas says:

Brilliant ! Many thanks

Michael says:

The file /etc/postfix/main.cf should look like this:


recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map

Thanks!

Kappy says:

This didnt work for me? what am i doing wrong.

i created a file in /etc/postfix/ called recipient_canonical_map
with the following entry “/./ example@example.com

then added this to main.cf
recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map

and then tested email send via “mail -s “hello” example@example.com

Didnt work, Also did postmap recipient_canonical_map

still doesnt send to the single email.

Bill says:

Thanks for the article, I have one question though:

this will intercept all outgoing emails from this server, thus rendering the server unusable for anything other than development. Right?

Is there a way that I can redirect only emails send by a specific address (login).
I want all email sent from dev@example.com to be delivered to trap@example.com, but I want normal users to be able to send/receive their emails (people like foo@example.com and bar@example.com shouldn’t be affected that is).

Can such a thing happen with postfix?

Thanks

Leave a Reply