Commit ab198312 authored by Barry Warsaw's avatar Barry Warsaw

The basics of 'mailman members --add'.

parent 82affb26
......@@ -89,7 +89,7 @@ def add_member(mlist, address, realname, password, delivery_mode, language):
user = config.db.user_manager.create_user()
user.real_name = (realname if realname else address_obj.real_name)
# Since created the user, then the member, and set preferences on the
# Since created the user, then the member, and set preferences on the
# appropriate object.
user.password = password
user.preferences.preferred_language = language
......@@ -25,10 +25,17 @@ __all__ = [
import sys
import codecs
from email.utils import parseaddr
from zope.interface import implements
from import add_member
from mailman.config import config
from mailman.i18n import _
from mailman.interfaces.command import ICLISubCommand
from mailman.interfaces.member import DeliveryMode
......@@ -41,6 +48,43 @@ class Members:
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
'-a', '--add',
help=_('Add all member addresses in FILENAME. FILENAME can be '
"'-' to indicate standard input."))
# Required positional argument.
'listname', metavar='LISTNAME', nargs=1,
The 'fully qualified list name', i.e. the posting address of the
mailing list. It must be a valid email address and the domain
must be registered with Mailman. List names are forced to lower
def process(self, args):
"""See `ICLISubCommand`."""
assert len(args.listname) == 1, (
'Unexpected positional arguments: %s' % args.listname)
fqdn_listname = args.listname[0]
mlist = config.db.list_manager.get(fqdn_listname)
if mlist is None:
self.parser.error(_('No such list: $fqdn_listname'))
if args.filename == '-':
fp = sys.stdin
fp =, 'r', 'utf-8')
for line in fp:
real_name, email = parseaddr(line)
# If not given in the input data, parseaddr() will return the
# empty string, as opposed to the empty unicode. We need a
# unicode real name here.
if real_name == '':
real_name = u''
add_member(mlist, email, real_name, None,
DeliveryMode.regular, mlist.preferred_language.code)
if fp is not sys.stdin:
Adding members
You can add members to a mailing list from the command line.
>>> from import create_list
>>> mlist = create_list('[email protected]')
>>> class FakeArgs:
... filename = None
... listname = None
>>> args = FakeArgs()
>>> from mailman.commands.cli_members import Members
>>> command = Members()
To do so, you need a file containing email addresses and full names that can
be parsed by email.utils.parseaddr().
>>> addresses = [
... ]
>>> import os
>>> path = os.path.join(config.VAR_DIR, 'addresses.txt')
>>> with open(path, 'w') as fp:
... for address in ('[email protected]',
... 'Bart Person <[email protected]>',
... '[email protected] (Cate Person)',
... ):
... print >> fp, address
>>> args.filename = path
>>> args.listname = [mlist.fqdn_listname]
>>> command.process(args)
>>> sorted(address.address for address in mlist.members.addresses)
[u'[email protected]', u'[email protected]', u'[email protected]']
You can also specify '-' as the filename, in which case the addresses are
taken from standard input.
>>> from StringIO import StringIO
>>> fp = StringIO()
>>> for address in ('[email protected]',
... 'Elly Person <[email protected]>',
... '[email protected] (Fred Person)',
... ):
... print >> fp, address
>>> import sys
>>> sys.stdin = fp
>>> args.filename = '-'
>>> command.process(args)
>>> sys.stdin = sys.__stdin__
>>> sorted(address.address for address in mlist.members.addresses)
[u'[email protected]', u'[email protected]', u'[email protected]',
u'[email protected]', u'[email protected]', u'[email protected]']
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment