Postfix – Ignore Specific User in sender_bcc_maps

postfix

I have sender_bcc_maps set up and working with postfix so that outgoing mail gets bcc'd to the sender's address. My configuration is more or less:

main.cf

sender_bcc_maps = regexp:/etc/postfix/regexp_sender_bcc

regexp_sender_bcc

/^([^@]+)@[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+$/ [email protected]

This works great. Now I have an email address [email protected] that I'd like to exclude from this configuration.

Idea 1: I first tried adding a row to route to a non-existent mailbox

/^nobody.*$/ [email protected]

Predictably, this results in an attempt to bcc that address and a bounceback since it doesn't exist.

Idea 2: Next I tried simply changing the target to an empty string:

/^nobody.*$/

This generates a warning and completely drops the outgoing mail as well:

warning: sender_bcc_maps lookup of [email protected] returns an empty string result
warning: sender_bcc_maps should return NO RESULT in case of NOT FOUND
warning: sender_bcc_maps map lookup problem -- message not accepted, try again later

Idea 3: I then tried routing this mail to a local user:

/^nobody.*$/ nobody@localhost

This somewhat does the job but then all these messages are still delivered to the local mail system.

I'd like to do either one of these – preferring the former:

  • configure sender_bcc_maps to completely ignore a specific sender's address
  • configure postfix to completely discard mail to a specific user

Best Answer

Configure sender_bcc_maps to completely ignore a specific sender's address:

(Credit to Viktor Dukhovni who answered this part of the question via postfix-users)

The Postfix regular expression table is capable of conditional negated pattern matching by using if !/pattern/flags ... endif. In my case the following accomplishes what I am after:

if !/^nobody@/
/^([^@]+)@[a-z-]+\.[a-z]+/ [email protected]
endif

There are also some small improvements to the domain matching part of the pattern since, as was pointed out, the regex is case insensitive by default.

Configure postfix to completely discard mail to a specific user:

The following worked for me to silently discard mail for the specified user. The server is running Ubuntu 20.04.3 LTS but this should work for nearly any Debian flavor:

In /etc/postfix/main.cf:

transport_maps = hash:/etc/postfix/transport

In /etc/postfix/transport:

[email protected] discard:

Then in a shell run:

postmap /etc/postfix/transport
service postfix restart