File recipients

Mailman can calculate the recipients for a message from a Sendmail-style include file. This file must be called members.txt and it must live in the list’s data directory.

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

Short circuiting

If the message’s metadata already has recipients, this handler immediately returns.

>>> msg = message_from_string("""\
... From: [email protected]
...
... A message.
... """)
>>> msgdata = {'recipients': 7}

>>> handler = config.handlers['file-recipients']
>>> handler.process(mlist, msg, msgdata)
>>> print(msg.as_string())
From: [email protected]

A message.

>>> dump_msgdata(msgdata)
recipients: 7

Existing file

If the file exists, it contains a list of addresses, one per line. These addresses are returned as the set of recipients.

>>> import os
>>> file_path = os.path.join(mlist.data_path, 'members.txt')
>>> with open(file_path, 'w', encoding='utf-8') as fp:
...     print('[email protected]', file=fp)
...     print('[email protected]', file=fp)
...     print('[email protected]', file=fp)
...     print('[email protected]', file=fp)
...     print('[email protected]', file=fp)
...     print('[email protected]', file=fp)

>>> msgdata = {}
>>> handler.process(mlist, msg, msgdata)
>>> dump_list(msgdata['recipients'])
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

However, if the sender of the original message is a member of the list and their address is in the include file, the sender’s address is not included in the recipients list.

>>> from mailman.interfaces.usermanager import IUserManager
>>> from zope.component import getUtility
>>> address_1 = getUtility(IUserManager).create_address(
...     '[email protected]')

>>> from mailman.interfaces.member import MemberRole
>>> mlist.subscribe(address_1, MemberRole.member)
<Member: [email protected] on [email protected] as MemberRole.member>

>>> msg = message_from_string("""\
... From: [email protected]
...
... A message.
... """)
>>> msgdata = {}
>>> handler.process(mlist, msg, msgdata)
>>> dump_list(msgdata['recipients'])
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]