The mailing list manager

The IListManager is how you create, delete, and retrieve mailing list objects.

>>> from mailman.interfaces.listmanager import IListManager
>>> from zope.component import getUtility
>>> list_manager = getUtility(IListManager)

Creating a mailing list

Creating the list returns the newly created IMailList object.

>>> from mailman.interfaces.mailinglist import IMailingList
>>> mlist = list_manager.create('')
>>> IMailingList.providedBy(mlist)

All lists with identities have a short name, a host name, a fully qualified listname, and an RFC 2369 list id. This latter will not change even if the mailing list moves to a different host, so it is what uniquely distinguishes the mailing list to the system.

>>> print(mlist.list_name)
>>> print(mlist.mail_host)
>>> print(mlist.fqdn_listname)
>>> print(mlist.list_id)

Deleting a mailing list

Use the list manager to delete a mailing list.

>>> list_manager.delete(mlist)
>>> sorted(list_manager.names)

After deleting the list, you can create it again.

>>> mlist = list_manager.create('')
>>> print(mlist.fqdn_listname)

Retrieving a mailing list

When a mailing list exists, you can ask the list manager for it and you will always get the same object back.

>>> list_manager.get('')
<mailing list "" at ...>

The .get() method is ambidextrous, so it also accepts List-ID’s.

>>> list_manager.get('')
<mailing list "" at ...>

You can get a mailing list specifically by its List-ID.

>>> list_manager.get_by_list_id('')
<mailing list "" at ...>

And you can get a mailing list specifically by its fully-qualified list name.

>>> list_manager.get_by_fqdn('')
<mailing list "" at ...>

If you try to get a list that doesn’t existing yet, you get None.

>>> print(list_manager.get(''))
>>> print(list_manager.get_by_list_id(''))

You also get None if the list name is invalid.

>>> print(list_manager.get('foo'))

Iterating over all mailing lists

Once you’ve created a bunch of mailing lists, you can use the list manager to iterate over the mailing list objects, the list posting addresses, or the list address components.

>>> mlist_3 = list_manager.create('')
>>> mlist_4 = list_manager.create('')

>>> for name in sorted(list_manager.names):
...     print(name)

>>> for list_id in sorted(list_manager.list_ids):
...     print(list_id)

>>> for fqdn_listname in sorted(m.fqdn_listname
...                             for m in list_manager.mailing_lists):
...     print(fqdn_listname)

>>> for list_name, mail_host in sorted(list_manager.name_components):
...     print(list_name, '@', mail_host)
ant @
cat @
dog @