Silently drop emails to specific invalid domain in Postfix?

Justin Lloyd asked:

What is the best way to have Postfix silently drop emails to a specific invalid domain?

For reasons that cannot be handled otherwise right now, our mail servers receive a lot of emails for addresses of form $userid@BLAH, where @BLAH is always that value and $userid is programmatically-generated by the sender. The sender mail servers are allowed to relay through our mail servers but since BLAH is not a valid domain, the messages get deferred with “Host or domain name not found. Name service error for name=BLAH type=MX: Host not found, try again”. Obviously header_checks doesn’t work since that’s handled well after the hostname lookup.

I’d like to prevent these from ever getting to the queue, even though they do eventually age out after a few retries, and just silently discard them instead. (Eventually we’ll get the source issue resolved but that may not be any time soon.)

I adapted Michael’s recommendation as the virtual mailbox configuration was delivering to the mailbox rather than /dev/null despite the alias. However, I was already virtual aliasing another domain, so I just added the BLAH “domain” to virtual_alias_domains and added @BLAH BLAH to /etc/postfix/virtual, postmapped, and along with the new /etc/aliases entry, it works like a charm.

Update 2
It occurred to me that with the addition of BLAH to virtual_alias_domains, a header_check rule should now work, obviating the need for the aliases in virtual alias aliases, and testing does bear this out. This has the added benefit of the emails now being logged as discards and thus picked up by the existing patterns in our Collectd tail plugin configuration for our SMTP server metrics graphs.

My answer:

First, you’ll need to accept mail for the nonexistent domain, e.g. by adding it to virtual_mailbox_domains.

Then you’ll need to make a catchall address in virtual_mailbox_maps that delivers it locally, for example:    throwaway

Last you need to make a local alias for throwaway in /etc/aliases that delivers mail to /dev/null, e.g.:

throwaway       /dev/null

Postmap and reload postfix as appropriate.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.