How to configure postfix to throw away all email to one domain and relay all other email?

I need to set up postfix in a development/test environment to filter e-mails so we don't spam our customers. In our test environments we scrub all our user data so that e-mail addresses are changed to @localhost, although some addresses might get changed to valid e-mail addresses at a later point for testing purposes. We relay all our email through a third party provider for delivery, so what I'd like to do is set up postfix to:

  1. Throw out any e-mail sent to localhost
  2. Relay all remaining e-mail to our third party provider.

Being relatively new to postfix, what would be the easiest way to set this up?


Well I seem to have managed to figure this out with a bit of searching & testing. Here's what I had to do:

  • In /etc/postfix/main.cf:

    transport_maps = hash:/etc/postfix/transport
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = static:<relayhost username>:<relayhost password>
    smtp_sasl_security_options = noanonymous
    smtp_tls_security_level = may
    start_tls = yes
    
  • In /etc/postfix/transport:

    localhost discard: 
    localhost.localdomain discard:
    * relay:[smtp.relayhost.com]:587
    

Relaying apparently also bypasses the alias_maps directive, so for aliases to continue working I had to comment out both alias_maps and alias_database, and replace them with virtual_alias_maps. The format of the virutal_alias_map is identical to alias_maps, so that was an easy change to make.

With these changes in place just restart postfix and also run "postmap /etc/postfix/transport" to build transport.db. Now everything addressed to @localhost or @localhost.localdomain is discarded while everything else is relayed through the specified host.