Drop [almost] all outbound mail in postfix for tests

yaroslaff

Yaroslav Polyakov

Posted on January 12, 2023

Drop [almost] all outbound mail in postfix for tests

Why to drop mail?

While troubleshooting mailman installation, I've come to a problem: I need to do many tests, but I do not want to disturb recipients of maillist with dozens of test messages. I do not know, if mailman3 has any feature for this kind of testing, but postfix has.

How to drop postfix mail except to whitelisted addresses

First, make sure, you have transport map enabled.

Usually, this line could look like this:

transport_maps = hash:/etc/postfix/transport
Enter fullscreen mode Exit fullscreen mode

Now, create other transport file, my /etc/postfix/blackhole:

mydomain.net :
mymailbox@gmail.com :
* discard:
Enter fullscreen mode Exit fullscreen mode

With this config postfix will deliver to *@mydomain.net and to mymailbox@gmail.com.

run postmap blackhole to generate /etc/postfix/blackhole.db.

add it to postfix transport with comma (if you already has transport map):

transport_maps = hash:/path/to/other/transport/map, hash:/etc/postfix/blackhole
Enter fullscreen mode Exit fullscreen mode

(or as single element, if you had no transport_maps).

Restart postfix: postfix reload. You're ready.

Testing

I sent few mails and here is what I have in logs:

Jan 12 09:53:17 list2 postfix/smtp[62239]: DB97418C0808: to=<mymailbox@gmail.com>, relay=gmail-smtp-in.l.google.com[173.194.76.27]:25, delay=0.41, delays=0.03/0.02/0.14/0.22, dsn=2.0.0, status=sent (250 2.0.0 OK  1673517197 g15-20020a056000118f00b002bdd88acc3dsi514695wrx.503 - gsmtp)

Jan 12 09:53:34 list2 postfix/discard[62251]: 7943118C0808: to=<test@test.com>, relay=none, delay=0.02, delays=0.02/0/0/0, dsn=2.0.0, status=sent (test.com)
Enter fullscreen mode Exit fullscreen mode

First is log line for real delivery (relay= remote mailserver) and second is log line for blackhole delivery, relay=none).

💖 💪 🙅 🚩
yaroslaff
Yaroslav Polyakov

Posted on January 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related