Banning email addresses

Email addresses can be banned from ever subscribing, either to a specific mailing list or globally within the Mailman system. Both explicit email addresses and email address patterns can be banned.

Bans are managed through the Ban Manager. There are ban managers for specific lists, and there is a global ban manager. To get access to the global ban manager, adapt None.

>>> from mailman.interfaces.bans import IBanManager
>>> global_bans = IBanManager(None)

At first, no email addresses are banned globally.

>>> global_bans.is_banned('[email protected]')
False

To get a list-specific ban manager, adapt the mailing list object.

>>> mlist = create_list('[email protected]')
>>> test_bans = IBanManager(mlist)

There are no bans for this particular list.

>>> test_bans.is_banned('[email protected]')
False

Specific bans

An email address can be banned from a specific mailing list by adding a ban to the list’s ban manager.

>>> test_bans.ban('[email protected]')
>>> test_bans.is_banned('[email protected]')
True
>>> test_bans.is_banned('[email protected]')
False

However, this is not a global ban.

>>> global_bans.is_banned('[email protected]')
False

Global bans

An email address can be banned globally, so that it cannot be subscribed to any mailing list.

>>> global_bans.ban('[email protected]')

Because there is a global ban, Dave is also banned from the mailing list.

>>> test_bans.is_banned('[email protected]')
True

Even when a new mailing list is created, Dave is still banned from this list because of his global ban.

>>> sample = create_list('[email protected]')
>>> sample_bans = IBanManager(sample)
>>> sample_bans.is_banned('[email protected]')
True

Dave is of course banned globally.

>>> global_bans.is_banned('[email protected]')
True

Cris however is not banned globally.

>>> global_bans.is_banned('[email protected]')
False

Even though Cris is not banned globally, we can add a global ban for her.

>>> global_bans.ban('[email protected]')
>>> global_bans.is_banned('[email protected]')
True

Cris is now banned from all mailing lists.

>>> test_bans.is_banned('[email protected]')
True
>>> sample_bans.is_banned('[email protected]')
True

We can remove the global ban to once again just ban her address from just the test list.

>>> global_bans.unban('[email protected]')
>>> global_bans.is_banned('[email protected]')
False
>>> test_bans.is_banned('[email protected]')
True
>>> sample_bans.is_banned('[email protected]')
False

Regular expression bans

Entire email address patterns can be banned, both for a specific mailing list and globally, just as specific addresses can be banned. Use this for example, when an entire domain is a spam faucet. When using a pattern, the email address must start with a caret (^).

>>> test_bans.ban('^.*@example.org')

Now, no one from example.org can subscribe to the test mailing list.

>>> test_bans.is_banned('[email protected]')
True
>>> test_bans.is_banned('[email protected]')
True

example.com addresses are not banned.

>>> test_bans.is_banned('[email protected]')
False

example.org addresses are not banned globally, nor for any other mailing list.

>>> sample_bans.is_banned('[email protected]')
False
>>> global_bans.is_banned('[email protected]')
False

Of course, we can ban everyone from example.org globally too.

>>> global_bans.ban('^.*@example.org')
>>> sample_bans.is_banned('[email protected]')
True
>>> global_bans.is_banned('[email protected]')
True

We can remove the mailing list ban on the pattern, though the global ban will still be in place.

>>> test_bans.unban('^.*@example.org')
>>> test_bans.is_banned('[email protected]')
True
>>> sample_bans.is_banned('[email protected]')
True
>>> global_bans.is_banned('[email protected]')
True

But once the global ban is removed, everyone from example.org can subscribe to the mailing lists.

>>> global_bans.unban('^.*@example.org')
>>> test_bans.is_banned('[email protected]')
False
>>> sample_bans.is_banned('[email protected]')
False
>>> global_bans.is_banned('[email protected]')
False

Adding and removing bans

It is not an error to add a ban more than once. These are just ignored.

>>> test_bans.ban('[email protected]')
>>> test_bans.ban('[email protected]')
>>> test_bans.is_banned('[email protected]')
True

Nor is it an error to remove a ban more than once.

>>> test_bans.unban('[email protected]')
>>> test_bans.unban('[email protected]')
>>> test_bans.is_banned('[email protected]')
False