Personalized decoration

Personalized messages can be decorated by headers and footers containing information specific to the recipient.

>>> from mailman.mta.decorating import DecoratingDelivery
>>> decorating = DecoratingDelivery()

Delivery strategies must implement the proper interface.

>>> from mailman.interfaces.mta import IMailTransportAgentDelivery
>>> from zope.interface.verify import verifyObject
>>> verifyObject(IMailTransportAgentDelivery, decorating)
True

Decorations

Decorations are added when the mailing list had a header and/or footer defined, and the decoration handler is told to do personalized decorations. We start by writing the site-global header and footer template.

>>> import os, tempfile
>>> template_dir = tempfile.mkdtemp()
>>> site_dir = os.path.join(template_dir, 'site', 'en')
>>> os.makedirs(site_dir)
>>> config.push('templates', """
... [paths.testing]
... template_dir: {}
... """.format(template_dir))

>>> myheader_path = os.path.join(site_dir, 'myheader.txt')
>>> with open(myheader_path, 'w') as fp:
...     print("""\
... Delivery address: $user_address
... Subscribed address: $user_delivered_to
... """, file=fp)
>>> myfooter_path = os.path.join(site_dir, 'myfooter.txt')
>>> with open(myfooter_path, 'w') as fp:
...     print("""\
... User name: $user_name
... Language: $user_language
... """, file=fp)

Then create a mailing list which will use this header and footer. Because these are site-global templates, we can use a shorted URL.

>>> mlist = create_list('[email protected]')
>>> from mailman.interfaces.template import ITemplateManager
>>> from zope.component import getUtility
>>> manager = getUtility(ITemplateManager)
>>> manager.set('list:member:regular:header', mlist.list_id,
...             'mailman:///myheader.txt')
>>> manager.set('list:member:regular:footer', mlist.list_id,
...             'mailman:///myfooter.txt')
>>> transaction.commit()
>>> msg = message_from_string("""\
... From: [email protected]
... To: [email protected]
... Subject: test one
... Message-ID: <aardvark>
...
... This is a test.
... """)
>>> recipients = {
...     '[email protected]',
...     '[email protected]',
...     '[email protected]',
...     }
>>> msgdata = dict(
...     recipients=recipients,
...     personalize=True,
...     )

More information is included when the recipient is a member of the mailing list.

>>> from mailman.interfaces.member import MemberRole
>>> from mailman.interfaces.usermanager import IUserManager
>>> user_manager = getUtility(IUserManager)

>>> anne = user_manager.create_user('[email protected]', 'Anne Person')
>>> mlist.subscribe(list(anne.addresses)[0], MemberRole.member)
<Member: Anne Person <[email protected]> ...

>>> bart = user_manager.create_user('[email protected]', 'Bart Person')
>>> mlist.subscribe(list(bart.addresses)[0], MemberRole.member)
<Member: Bart Person <[email protected]> ...

>>> cris = user_manager.create_user('[email protected]', 'Cris Person')
>>> mlist.subscribe(list(cris.addresses)[0], MemberRole.member)
<Member: Cris Person <[email protected]> ...

The decorations happen when the message is delivered.

>>> decorating.deliver(mlist, msg, msgdata)
{}
>>> messages = list(smtpd.messages)
>>> len(messages)
3

>>> from operator import itemgetter
>>> for message in sorted(messages, key=itemgetter('x-rcptto')):
...     print(message.as_string())
...     print('----------')
From: [email protected]
To: [email protected]
Subject: test one
Message-ID: <aardvark>
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Peer: ...
X-MailFrom: [email protected]
X-RcptTo: [email protected]

Delivery address: [email protected]
Subscribed address: [email protected]
This is a test.
User name: Anne Person
Language: English (USA)
----------
From: [email protected]
To: [email protected]
Subject: test one
Message-ID: <aardvark>
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Peer: ...
X-MailFrom: [email protected]
X-RcptTo: [email protected]

Delivery address: [email protected]
Subscribed address: [email protected]
This is a test.
User name: Bart Person
Language: English (USA)
----------
From: [email protected]
To: [email protected]
Subject: test one
Message-ID: <aardvark>
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Peer: ...
X-MailFrom: [email protected]
X-RcptTo: [email protected]le.com

Delivery address: [email protected]
Subscribed address: [email protected]
This is a test.
User name: Cris Person
Language: English (USA)
----------

Decorate only once

Do not decorate a message twice. Decorators must insert the decorated key into the message metadata.

>>> msgdata['nodecorate'] = True
>>> decorating.deliver(mlist, msg, msgdata)
{}
>>> messages = list(smtpd.messages)
>>> len(messages)
3

>>> for message in sorted(messages, key=itemgetter('x-rcptto')):
...     print(message.as_string())
...     print('----------')
From: [email protected]
To: [email protected]
Subject: test one
Message-ID: <aardvark>
X-Peer: ...
X-MailFrom: [email protected]
X-RcptTo: [email protected]

This is a test.
----------
From: [email protected]
To: [email protected]
Subject: test one
Message-ID: <aardvark>
X-Peer: ...
X-MailFrom: [email protected]
X-RcptTo: [email protected]

This is a test.
----------
From: [email protected]
To: [email protected]
Subject: test one
Message-ID: <aardvark>
X-Peer: ...
X-MailFrom: [email protected]
X-RcptTo: [email protected]

This is a test.
----------