6

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.

IanBussieres
  • 175
  • 1
  • 6

3 Answers3

9

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:
lsmooth
  • 1,541
  • 1
  • 9
  • 18
  • Doesn't seem to have worked... is there already supposed to be a 'transport' file ? All my emails are blocked, including the @example.com ones. – IanBussieres Feb 02 '12 at 21:27
  • That depends on the main.cf before you added the transport_maps above. If there is no transport_maps in the main.cf is by default empty. If there is one remove that one so only the added on remains. What do you mean by "All my emails are blocked" ? Do you get bounces, are they just not getting delivered? Do they pile up in postqueue? Or are they rejected directly when trying to send them? – lsmooth Feb 02 '12 at 23:46
  • There were no transport_maps before adding the one you suggest. Regarding the blocked emails, they hang in the queue: -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- 5D2BB58076 223 Fri Feb 3 10:06:28 root email@example.com – IanBussieres Feb 03 '12 at 15:08
  • I'm sorry to bump this @Ismooth, I just wanted to make sure you had posted the answers to your follow-up questions. I've tested once more, the problem persists. – IanBussieres Feb 07 '12 at 21:50
  • Ah sorry, is there also a reason listed in postqueue -p ? – lsmooth Feb 07 '12 at 23:49
  • Not a reason, specifically, it just hangs there : -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- 4987758077 318 Fri Feb 10 13:19:28 username username@example.com – IanBussieres Feb 10 '12 at 18:20
1

if example.com is your internal domain you want to edit the transport file to include just

example.com :

and not

example.com smtp:

per http://www.postfix.org/transport.5.html

otherwise with that one change this worked great for me. Thanks lsmooth!

ginog
  • 11
  • 1
  • For some reason this isn't working for me either... But as soon as I comment the line 'transport_maps = texthash:/etc/postfix/transport' in main.cf it starts working fine. – IanBussieres Feb 10 '12 at 18:37
-1

This looks incorrect. I used this setup to limit outgoing domains.

main.cf: transport_maps=hash:/etc/postfix/transport

and transport contains following lines:

domain1.foo smtp:ip.address.of.relay.host
domain2.foo smtp:ip.address.of.relay.host
* discard:

and then just postmap /etc/postfix/transport and reload the postfix. that worked fine for me.

Eero