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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
Sending Mail
============
simple_mail can be used to send mail easily:
>>> from lp.services.mail.sendmail import simple_sendmail
>>> msgid = simple_sendmail(
... from_addr='foo.bar@canonical.com',
... to_addrs='test@canonical.com',
... subject=u'Subject',
... body=u'Content')
The mail get sent when the transaction gets commited:
>>> import transaction
>>> transaction.commit()
Now let's look at the sent email:
>>> import email
>>> from lp.services.mail import stub
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> msg['To']
'test@canonical.com'
>>> msg['From']
'foo.bar@canonical.com'
>>> msg['Subject']
'Subject'
>>> msg.get_payload(decode=True)
'Content'
>>> # Make sure bulk headers are set for vacation programs
>>> msg['Precedence']
'bulk'
In cases where the sender is a Person with a preferred email address,
it's better to use simple_sendmail_from_person. It works just like
simple_sendmail, except that it expects a person instead of an address.
simple_sendmail_from_person has the advantage that it makes sure that
the person's name is encoded properly.
>>> from lp.services.mail.sendmail import simple_sendmail_from_person
>>> from lp.registry.interfaces.person import IPersonSet
>>> foo_bar = getUtility(IPersonSet).getByEmail('foo.bar@canonical.com')
>>> msgid = simple_sendmail_from_person(
... person=foo_bar,
... to_addrs='test@canonical.com',
... subject=u'Subject',
... body=u'Content')
>>> transaction.commit()
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> msg['To']
'test@canonical.com'
>>> msg['From']
'Foo Bar <foo.bar@canonical.com>'
>>> msg['Subject']
'Subject'
>>> msg.get_payload(decode=True)
'Content'
>>> msg['Precedence']
'bulk'
simple_sendmail_from_person uses the Person's preferred email address:
>>> login('test@canonical.com')
>>> from lp.services.identity.interfaces.emailaddress import (
... IEmailAddressSet)
>>> sample_person = getUtility(IPersonSet).getByEmail(
... 'test@canonical.com')
>>> testing = getUtility(IEmailAddressSet).getByEmail(
... 'testing@canonical.com')
>>> sample_person.setPreferredEmail(testing)
>>> sample_person.preferredemail.email
u'testing@canonical.com'
>>> msgid = simple_sendmail_from_person(
... person=sample_person,
... to_addrs='test@canonical.com',
... subject=u'Subject',
... body=u'Content')
>>> transaction.commit()
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> msg['From']
'Sample Person <testing@canonical.com>'
To make a header appear more than once in the sent message (e.g.
X-Launchpad-Bug), you can pass a headers dict whose keys are the header names
and whose values are the header body values. If a value is a list or a tuple,
the header will appear more than once in the output message.
>>> msgid = simple_sendmail(
... from_addr='foo.bar@canonical.com',
... to_addrs='test@canonical.com',
... subject=u'Subject',
... body=u'Content',
... headers={
... 'X-Foo': "test", 'X-Bar': ["first value", "second value"]})
>>> transaction.commit()
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> msg["X-Foo"]
'test'
>>> msg.get_all("X-Bar")
['first value', 'second value']
simple_sendmail accepts the subject and body as unicode strings, but
the from_addr and to_addrs have to be str objects containing ASCII
only.
>>> msgid = simple_sendmail(
... from_addr='Foo Bar <foo.bar@canonical.com>',
... to_addrs='Sample Person <test@canonical.com>',
... subject=u'\xc4mnesrad',
... body=u'Inneh\xe5ll')
>>> transaction.commit()
Now let's look at the sent email again.
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> from email.Header import decode_header
>>> subject_str, charset = decode_header(msg['Subject'])[0]
>>> subject_str.decode(charset)
u'\xc4mnesrad'
>>> msg.get_payload(decode=True).decode(msg.get_content_charset())
u'Inneh\xe5ll'
If we use simple_sendmail_from_person, the person's displayname can
contain non-ASCII characters:
>>> login('foo.bar@canonical.com')
>>> foo_bar.displayname = u'F\xf6\xf6 B\u0105r'
>>> msgid = simple_sendmail_from_person(
... person=foo_bar,
... to_addrs='Sample Person <test@canonical.com>',
... subject=u'\xc4mnesrad',
... body=u'Inneh\xe5ll')
>>> transaction.commit()
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> from email.Utils import parseaddr
>>> from_name_encoded, from_addr = parseaddr(msg['From'])
>>> from_name_str, charset = decode_header(from_name_encoded)[0]
>>> from_addr
'foo.bar@canonical.com'
>>> from_name_str.decode(charset)
u'F\xf6\xf6 B\u0105r'
>>> subject_str, charset = decode_header(msg['Subject'])[0]
>>> subject_str.decode(charset)
u'\xc4mnesrad'
>>> msg.get_payload(decode=True).decode(msg.get_content_charset())
u'Inneh\xe5ll'
simple_sendmail_from_person also makes sure that the name gets
surrounded by quotes and quoted if necessary:
>>> login('foo.bar@canonical.com')
>>> foo_bar.displayname = u'Foo [Baz] " Bar'
>>> msgid = simple_sendmail_from_person(
... person=foo_bar,
... to_addrs='Sample Person <test@canonical.com>',
... subject=u'\xc4mnesrad',
... body=u'Inneh\xe5ll')
>>> transaction.commit()
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> print msg['From']
"Foo \[Baz\] \" Bar" <foo.bar@canonical.com>
If we pass a unicode object to send_mail, it will try and covert it. If a
non-ASCII str object is passed, it will throw a UnicodeDecodeError.
>>> simple_sendmail(
... from_addr=u'foo.bar@canonical.com',
... to_addrs='test@canonical.com',
... subject=u'Subject',
... body=u'Content')
'20...launchpad@...'
>>> simple_sendmail(
... from_addr='F\xf4\xf4 Bar <foo.bar@canonical.com>',
... to_addrs='test@canonical.com',
... subject=u'Subject',
... body=u'Content')
Traceback (most recent call last):
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf4 in position 1:
ordinal not in range(128)
>>> simple_sendmail(
... from_addr='foo.bar@canonical.com',
... to_addrs=u'test@canonical.com',
... subject=u'Subject',
... body=u'Content')
'20...launchpad@...'
>>> simple_sendmail(
... from_addr='Foo Bar <foo.bar@canonical.com>',
... to_addrs=['S\xc4\x85mple Person <test@canonical.com>'],
... subject=u'Subject',
... body=u'Content')
Traceback (most recent call last):
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 1:
ordinal not in range(128)
>>> transaction.abort()
Passing `bulk=False` to simple_sendmail disables the adding of the bulk
precedence header to the email's headers.
>>> msgid = simple_sendmail(
... from_addr='feedback@launchpad.net',
... to_addrs='test@canonical.com',
... subject=u'Forgot password',
... body=u'Content',
... bulk=False)
>>> transaction.commit()
The message is the same as the one from the simple_sendmail test except
that the precedence header was not added.
>>> from_addr, to_addr, raw_message = stub.test_emails.pop()
>>> msg = email.message_from_string(raw_message)
>>> msg['To']
'test@canonical.com'
>>> msg['From']
'feedback@launchpad.net'
>>> msg['Subject']
'Forgot password'
>>> msg.get_payload(decode=True)
'Content'
>>> print msg['Precedence']
None
sendmail
========
simple_sendmail creates a Message instance, and sends it via another
function, sendmail. sendmail() can also be used directly if you want to
send more complicated emails, like emails with attachments.
>>> from lp.services.mail.sendmail import sendmail
Let's send a mail using that function. We only create a simple message
to test with, though.
>>> msg = email.MIMEText.MIMEText("Some content")
>>> msg['From'] = 'foo.bar@canonical.com'
>>> msg['To'] = 'test@canonical.com'
>>> msg['Subject'] = "test"
>>> msgid = sendmail(msg)
>>> transaction.commit()
sendmail automatically adds Return-Path and Errors-To headers to
provide better bounce handling.
>>> from lp.services.config import config
>>> from_addr, to_add, raw_message = stub.test_emails.pop()
>>> sent_msg = email.message_from_string(raw_message)
>>> sent_msg['Return-Path'] == config.canonical.bounce_address
True
>>> sent_msg['Errors-To'] == config.canonical.bounce_address
True
It must also add a Precedence: bulk header so that automatic replies
(e.g. vacation programs) don't try to respond to them.
>>> sent_msg['Precedence']
'bulk'
It's possible to set Return-Path manually if needed.
>>> msg.replace_header('Return-Path', '<>')
>>> msgid = sendmail(msg)
>>> transaction.commit()
>>> from_addr, to_add, raw_message = stub.test_emails.pop()
>>> sent_msg = email.message_from_string(raw_message)
>>> sent_msg['Return-Path']
'<>'
If we want to bounce messages, we can manually specify which addresses
the mail should be sent to. When we do this, the 'To' and 'CC' headers
are ignored.
>>> msg = email.MIMEText.MIMEText("Some content")
>>> msg['From'] = 'foo.bar@canonical.com'
>>> msg['To'] = 'test@canonical.com'
>>> msg['CC'] = 'foo.bar@canonical.com'
>>> msg['Subject'] = "test"
>>> msgid = sendmail(msg, to_addrs=['no-priv@canonical.com'])
>>> transaction.commit()
>>> from_addr, to_addrs, raw_message = stub.test_emails.pop()
>>> to_addrs
['no-priv@canonical.com']
>>> sent_msg = email.message_from_string(raw_message)
>>> sent_msg['To']
'test@canonical.com'
>>> sent_msg['CC']
'foo.bar@canonical.com'
Since sendmail() gets the addresses to send to from the e-mail header,
it needs to take care of unfolding the headers, so that they don't
contain any line breaks.
>>> folded_message = email.message_from_string("""Subject: required
... From: Not used
... <from.address@example.com>
... To: To Address
... <to.address@example.com>
... CC: CC Address
... <cc.address@example.com>
...
... Content
... """)
>>> msgid = sendmail(folded_message)
>>> transaction.commit()
>>> from_addr, to_addrs, raw_message = stub.test_emails.pop()
>>> from_addr
'bounces@canonical.com'
>>> sorted(to_addrs)
['CC Address <cc.address@example.com>',
'To Address <to.address@example.com>']
|