Is it possible to limit outgoing emails in Postfix to one specific domain?

Our production servers need to mass-mail our clients on a regular basis. That works perfectly. The problem is that our 'staging' environment also sends out the emails to our clients when we test new features and forget to override the actual client emails throughout our numerous databases and tables.

A solution came to mind: could I configure Postfix in a way that all outgoing emails are blocked unless they are destined to our own domain when we are on our staging/test servers.

I googled around and found how to reject specific domains, but have not found the way to reject them all except one. Any help will be appreciated.


Solution 1:

You can add a transport map in main.cf:

transport_maps = texthash:/etc/postfix/transport

Then edit /etc/postfix/transport with your favorite editor and add this:

example.com smtp:
* error:only mail to *@example.com will be delivered

This will bounce every mail with recipients other than *@example.com. If you need to be able to change the transport_map on the fly use hash instead of texthash, but you have to use postmap on the file once you changed it to update the corresponding .db file and so postfix notices it has changed. If you don't want to bounce other mails use this instead:

example.com smtp:
* discard: