summaryrefslogtreecommitdiff
path: root/src/mailman/mta/docs/decorating.rst
blob: 55125642650583fec5af6227b5f8b0c11bed315e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
=======================
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('test@example.com')
    >>> 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: aperson@example.org
    ... To: test@example.com
    ... Subject: test one
    ... Message-ID: <aardvark>
    ...
    ... This is a test.
    ... """)

    >>> recipients = {
    ...     'aperson@example.com',
    ...     'bperson@example.com',
    ...     'cperson@example.com',
    ...     }

    >>> 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('aperson@example.com', 'Anne Person')
    >>> mlist.subscribe(list(anne.addresses)[0], MemberRole.member)
    <Member: Anne Person <aperson@example.com> ...

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

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

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: aperson@example.org
    To: test@example.com
    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: test-bounces@example.com
    X-RcptTo: aperson@example.com
    <BLANKLINE>
    Delivery address: aperson@example.com
    Subscribed address: aperson@example.com
    This is a test.
    User name: Anne Person
    Language: English (USA)
    ----------
    From: aperson@example.org
    To: test@example.com
    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: test-bounces@example.com
    X-RcptTo: bperson@example.com
    <BLANKLINE>
    Delivery address: bperson@example.com
    Subscribed address: bperson@example.com
    This is a test.
    User name: Bart Person
    Language: English (USA)
    ----------
    From: aperson@example.org
    To: test@example.com
    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: test-bounces@example.com
    X-RcptTo: cperson@example.com
    <BLANKLINE>
    Delivery address: cperson@example.com
    Subscribed address: cperson@example.com
    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: aperson@example.org
    To: test@example.com
    Subject: test one
    Message-ID: <aardvark>
    X-Peer: ...
    X-MailFrom: test-bounces@example.com
    X-RcptTo: aperson@example.com
    <BLANKLINE>
    This is a test.
    ----------
    From: aperson@example.org
    To: test@example.com
    Subject: test one
    Message-ID: <aardvark>
    X-Peer: ...
    X-MailFrom: test-bounces@example.com
    X-RcptTo: bperson@example.com
    <BLANKLINE>
    This is a test.
    ----------
    From: aperson@example.org
    To: test@example.com
    Subject: test one
    Message-ID: <aardvark>
    X-Peer: ...
    X-MailFrom: test-bounces@example.com
    X-RcptTo: cperson@example.com
    <BLANKLINE>
    This is a test.
    ----------

.. Clean up

    >>> config.pop('templates')
    >>> import shutil
    >>> shutil.rmtree(template_dir)