forked from pallets-eco/flask-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
703 lines (582 loc) · 27.7 KB
/
tests.py
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# -*- coding: utf-8 -*-
from __future__ import with_statement
import base64
import email
import unittest
import time
import re
import mock
from contextlib import contextmanager
from email.header import Header
from email import charset
from flask import Flask
from flask_mail import Mail, Message, BadHeaderError, sanitize_address, PY3
from speaklater import make_lazy_string
class TestCase(unittest.TestCase):
TESTING = True
MAIL_DEFAULT_SENDER = "[email protected]"
def setUp(self):
self.app = Flask(__name__)
self.app.config.from_object(self)
self.assertTrue(self.app.testing)
self.mail = Mail(self.app)
self.ctx = self.app.test_request_context()
self.ctx.push()
def tearDown(self):
self.ctx.pop()
@contextmanager
def mail_config(self, **settings):
"""
Context manager to alter mail config during a test and restore it after,
even in case of a failure.
"""
original = {}
state = self.mail.state
for key in settings:
assert hasattr(state, key)
original[key] = getattr(state, key)
setattr(state, key, settings[key])
yield
# restore
for k, v in original.items():
setattr(state, k, v)
def assertIn(self, member, container, msg=None):
if hasattr(unittest.TestCase, 'assertIn'):
return unittest.TestCase.assertIn(self, member, container, msg)
return self.assertTrue(member in container)
def assertNotIn(self, member, container, msg=None):
if hasattr(unittest.TestCase, 'assertNotIn'):
return unittest.TestCase.assertNotIn(self, member, container, msg)
return self.assertFalse(member in container)
def assertIsNone(self, obj, msg=None):
if hasattr(unittest.TestCase, 'assertIsNone'):
return unittest.TestCase.assertIsNone(self, obj, msg)
return self.assertTrue(obj is None)
def assertIsNotNone(self, obj, msg=None):
if hasattr(unittest.TestCase, 'assertIsNotNone'):
return unittest.TestCase.assertIsNotNone(self, obj, msg)
return self.assertTrue(obj is not None)
class TestInitialization(TestCase):
def test_init_mail(self):
mail = self.mail.init_mail(
self.app.config,
self.app.debug,
self.app.testing
)
self.assertEquals(self.mail.state.__dict__, mail.__dict__)
class TestMessage(TestCase):
def test_initialize(self):
msg = Message(subject="subject",
recipients=["[email protected]"])
self.assertEqual(msg.sender, self.app.extensions['mail'].default_sender)
self.assertEqual(msg.recipients, ["[email protected]"])
def test_recipients_properly_initialized(self):
msg = Message(subject="subject")
self.assertEqual(msg.recipients, [])
msg2 = Message(subject="subject")
msg2.add_recipient("[email protected]")
self.assertEqual(len(msg2.recipients), 1)
def test_esmtp_options_properly_initialized(self):
msg = Message(subject="subject")
self.assertEqual(msg.mail_options, [])
self.assertEqual(msg.rcpt_options, [])
msg = Message(subject="subject", mail_options=['BODY=8BITMIME'])
self.assertEqual(msg.mail_options, ['BODY=8BITMIME'])
msg2 = Message(subject="subject", rcpt_options=['NOTIFY=SUCCESS'])
self.assertEqual(msg2.rcpt_options, ['NOTIFY=SUCCESS'])
def test_sendto_properly_set(self):
msg = Message(subject="subject", recipients=["[email protected]"],
cc=["[email protected]"], bcc=["[email protected]"])
self.assertEqual(len(msg.send_to), 3)
msg.add_recipient("[email protected]")
self.assertEqual(len(msg.send_to), 3)
def test_add_recipient(self):
msg = Message("testing")
msg.add_recipient("[email protected]")
self.assertEqual(msg.recipients, ["[email protected]"])
def test_sender_as_tuple(self):
msg = Message(subject="testing",
sender=("tester", "[email protected]"))
self.assertEqual('tester <[email protected]>', msg.sender)
def test_default_sender_as_tuple(self):
self.app.extensions['mail'].default_sender = ('tester', '[email protected]')
msg = Message(subject="testing")
self.assertEqual('tester <[email protected]>', msg.sender)
def test_reply_to(self):
msg = Message(subject="testing",
recipients=["[email protected]"],
sender="spammer <[email protected]>",
reply_to="somebody <[email protected]>",
body="testing")
response = msg.as_string()
h = Header("Reply-To: %s" % sanitize_address('somebody <[email protected]>'))
self.assertIn(h.encode(), str(response))
def test_send_without_sender(self):
self.app.extensions['mail'].default_sender = None
msg = Message(subject="testing", recipients=["[email protected]"], body="testing")
self.assertRaises(AssertionError, self.mail.send, msg)
def test_send_without_recipients(self):
msg = Message(subject="testing",
recipients=[],
body="testing")
self.assertRaises(AssertionError, self.mail.send, msg)
def test_bcc(self):
msg = Message(sender="[email protected]",
subject="testing",
recipients=["[email protected]"],
body="testing",
bcc=["[email protected]"])
response = msg.as_string()
self.assertNotIn("[email protected]", str(response))
def test_cc(self):
msg = Message(sender="[email protected]",
subject="testing",
recipients=["[email protected]"],
body="testing",
cc=["[email protected]"])
response = msg.as_string()
self.assertIn("Cc: [email protected]", str(response))
def test_attach(self):
msg = Message(subject="testing",
recipients=["[email protected]"],
body="testing")
msg.attach(data=b"this is a test",
content_type="text/plain")
a = msg.attachments[0]
self.assertIsNone(a.filename)
self.assertEqual(a.disposition, 'attachment')
self.assertEqual(a.content_type, "text/plain")
self.assertEqual(a.data, b"this is a test")
def test_bad_header_subject(self):
msg = Message(subject="testing\r\n",
sender="[email protected]",
body="testing",
recipients=["[email protected]"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
def test_multiline_subject(self):
msg = Message(subject="testing\r\n testing\r\n testing \r\n \ttesting",
sender="[email protected]",
body="testing",
recipients=["[email protected]"])
self.mail.send(msg)
response = msg.as_string()
self.assertIn("From: [email protected]", str(response))
self.assertIn("testing\r\n testing\r\n testing \r\n \ttesting", str(response))
def test_bad_multiline_subject(self):
msg = Message(subject="testing\r\n testing\r\n ",
sender="[email protected]",
body="testing",
recipients=["[email protected]"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
msg = Message(subject="testing\r\n testing\r\n\t",
sender="[email protected]",
body="testing",
recipients=["[email protected]"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
msg = Message(subject="testing\r\n testing\r\n\n",
sender="[email protected]",
body="testing",
recipients=["[email protected]"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
def test_bad_header_sender(self):
msg = Message(subject="testing",
sender="[email protected]\r\n",
recipients=["[email protected]"],
body="testing")
self.assertIn('From: [email protected]', msg.as_string())
def test_bad_header_reply_to(self):
msg = Message(subject="testing",
sender="[email protected]",
reply_to="[email protected]\r",
recipients=["[email protected]"],
body="testing")
self.assertIn('From: [email protected]', msg.as_string())
self.assertIn('To: [email protected]', msg.as_string())
self.assertIn('Reply-To: [email protected]', msg.as_string())
def test_bad_header_recipient(self):
msg = Message(subject="testing",
sender="[email protected]",
recipients=[
"to\r\[email protected]"],
body="testing")
self.assertIn('To: [email protected]', msg.as_string())
def test_emails_are_sanitized(self):
msg = Message(subject="testing",
sender="sender\r\[email protected]",
reply_to="reply_to\r\[email protected]",
recipients=["recipient\r\[email protected]"])
self.assertIn('[email protected]', msg.as_string())
self.assertIn('[email protected]', msg.as_string())
self.assertIn('[email protected]', msg.as_string())
def test_plain_message(self):
plain_text = "Hello Joe,\nHow are you?"
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body=plain_text)
self.assertEqual(plain_text, msg.body)
self.assertIn('Content-Type: text/plain', msg.as_string())
def test_message_str(self):
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body="some plain text")
self.assertEqual(msg.as_string(), str(msg))
def test_plain_message_with_attachments(self):
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain")
self.assertIn('Content-Type: multipart/mixed', msg.as_string())
def test_plain_message_with_ascii_attachment(self):
msg = Message(subject="subject",
recipients=["[email protected]"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain",
filename='test doc.txt')
self.assertIn('Content-Disposition: attachment; filename="test doc.txt"', msg.as_string())
def test_plain_message_with_unicode_attachment(self):
msg = Message(subject="subject",
recipients=["[email protected]"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain",
filename=u'ünicöde ←→ ✓.txt')
parsed = email.message_from_string(msg.as_string())
self.assertIn(re.sub(r'\s+', ' ', parsed.get_payload()[1].get('Content-Disposition')), [
'attachment; filename*="UTF8\'\'%C3%BCnic%C3%B6de%20%E2%86%90%E2%86%92%20%E2%9C%93.txt"',
'attachment; filename*=UTF8\'\'%C3%BCnic%C3%B6de%20%E2%86%90%E2%86%92%20%E2%9C%93.txt'
])
def test_plain_message_with_ascii_converted_attachment(self):
with self.mail_config(ascii_attachments=True):
msg = Message(subject="subject",
recipients=["[email protected]"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain",
filename=u'ünicödeß ←.→ ✓.txt')
parsed = email.message_from_string(msg.as_string())
self.assertIn(
'Content-Disposition: attachment; filename="unicode . .txt"',
msg.as_string())
def test_html_message(self):
html_text = "<p>Hello World</p>"
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
html=html_text)
self.assertEqual(html_text, msg.html)
self.assertIn('Content-Type: multipart/alternative', msg.as_string())
def test_json_message(self):
json_text = '{"msg": "Hello World!}'
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
alts={'json': json_text})
self.assertEqual(json_text, msg.alts['json'])
self.assertIn('Content-Type: multipart/alternative', msg.as_string())
def test_html_message_with_attachments(self):
html_text = "<p>Hello World</p>"
plain_text = 'Hello World'
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body=plain_text,
html=html_text)
msg.attach(data=b"this is a test",
content_type="text/plain")
self.assertEqual(html_text, msg.html)
self.assertIn('Content-Type: multipart/alternative', msg.as_string())
parsed = email.message_from_string(msg.as_string())
self.assertEqual(len(parsed.get_payload()), 2)
body, attachment = parsed.get_payload()
self.assertEqual(len(body.get_payload()), 2)
plain, html = body.get_payload()
self.assertEqual(plain.get_payload(), plain_text)
self.assertEqual(html.get_payload(), html_text)
self.assertEqual(base64.b64decode(attachment.get_payload()), b'this is a test')
def test_date_header(self):
before = time.time()
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body="hello",
date=time.time())
after = time.time()
self.assertTrue(before <= msg.date <= after)
dateFormatted = email.utils.formatdate(msg.date, localtime=True)
self.assertIn('Date: ' + dateFormatted, msg.as_string())
def test_msgid_header(self):
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body="hello")
# see RFC 5322 section 3.6.4. for the exact format specification
r = re.compile(r"<\S+@\S+>").match(msg.msgId)
self.assertIsNotNone(r)
self.assertIn('Message-ID: ' + msg.msgId, msg.as_string())
def test_unicode_sender_tuple(self):
msg = Message(subject="subject",
sender=(u"ÄÜÖ → ✓", '[email protected]>'),
recipients=["[email protected]"])
self.assertIn('From: =?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <[email protected]>', msg.as_string())
def test_unicode_sender(self):
msg = Message(subject="subject",
sender=u'ÄÜÖ → ✓ <[email protected]>>',
recipients=["[email protected]"])
self.assertIn('From: =?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <[email protected]>', msg.as_string())
def test_unicode_headers(self):
msg = Message(subject="subject",
sender=u'ÄÜÖ → ✓ <[email protected]>',
recipients=[u"Ä <[email protected]>", u"Ü <[email protected]>"],
cc=[u"Ö <[email protected]>"])
response = msg.as_string()
a1 = sanitize_address(u"Ä <[email protected]>")
a2 = sanitize_address(u"Ü <[email protected]>")
h1_a = Header("To: %s, %s" % (a1, a2))
h1_b = Header("To: %s, %s" % (a2, a1))
h2 = Header("From: %s" % sanitize_address(u"ÄÜÖ → ✓ <[email protected]>"))
h3 = Header("Cc: %s" % sanitize_address(u"Ö <[email protected]>"))
# Ugly, but there's no guaranteed order of the recipieints in the header
try:
self.assertIn(h1_a.encode(), response)
except AssertionError:
self.assertIn(h1_b.encode(), response)
self.assertIn(h2.encode(), response)
self.assertIn(h3.encode(), response)
def test_unicode_subject(self):
msg = Message(subject=make_lazy_string(lambda a: a, u"sübject"),
sender='[email protected]',
recipients=["[email protected]"])
self.assertIn('=?utf-8?q?s=C3=BCbject?=', msg.as_string())
def test_extra_headers(self):
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body="hello",
extra_headers={'X-Extra-Header': 'Yes'})
self.assertIn('X-Extra-Header: Yes', msg.as_string())
def test_message_charset(self):
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
charset='us-ascii')
# ascii body
msg.body = "normal ascii text"
self.assertIn('Content-Type: text/plain; charset="us-ascii"', msg.as_string())
# ascii html
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
charset='us-ascii')
msg.body = None
msg.html = "<html><h1>hello</h1></html>"
self.assertIn('Content-Type: text/html; charset="us-ascii"', msg.as_string())
# unicode body
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"])
msg.body = u"ünicöde ←→ ✓"
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
# unicode body and unicode html
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"])
msg.html = u"ünicöde ←→ ✓"
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
self.assertIn('Content-Type: text/html; charset="utf-8"', msg.as_string())
# unicode body and attachments
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"])
msg.html = None
msg.attach(data=b"foobar", content_type='text/csv')
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
# unicode sender as tuple
msg = Message(sender=(u"送信者", "[email protected]"),
subject=u"表題",
recipients=["[email protected]"],
reply_to=u"返信先 <[email protected]>",
charset='shift_jis') # japanese
msg.body = u'内容'
self.assertIn('From: =?iso-2022-jp?', msg.as_string())
self.assertNotIn('From: =?utf-8?', msg.as_string())
self.assertIn('Subject: =?iso-2022-jp?', msg.as_string())
self.assertNotIn('Subject: =?utf-8?', msg.as_string())
self.assertIn('Reply-To: =?iso-2022-jp?', msg.as_string())
self.assertNotIn('Reply-To: =?utf-8?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="iso-2022-jp"', msg.as_string())
# unicode subject sjis
msg = Message(sender="[email protected]",
subject=u"表題",
recipients=["[email protected]"],
charset='shift_jis') # japanese
msg.body = u'内容'
self.assertIn('Subject: =?iso-2022-jp?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="iso-2022-jp"', msg.as_string())
# unicode subject utf-8
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
charset='utf-8')
msg.body = u'内容'
self.assertIn('Subject: subject', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
# ascii subject
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
charset='us-ascii')
msg.body = "normal ascii text"
self.assertNotIn('Subject: =?us-ascii?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="us-ascii"', msg.as_string())
# default charset
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"])
msg.body = "normal ascii text"
self.assertNotIn('Subject: =?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
def test_empty_subject_header(self):
msg = Message(sender="[email protected]",
recipients=["[email protected]"])
msg.body = "normal ascii text"
self.mail.send(msg)
self.assertNotIn('Subject:', msg.as_string())
class TestMail(TestCase):
def test_send(self):
with self.mail.record_messages() as outbox:
msg = Message(subject="testing",
recipients=["[email protected]"],
body="test")
self.mail.send(msg)
self.assertIsNotNone(msg.date)
self.assertEqual(len(outbox), 1)
sent_msg = outbox[0]
self.assertEqual(msg.sender, self.app.extensions['mail'].default_sender)
def test_send_message(self):
with self.mail.record_messages() as outbox:
self.mail.send_message(subject="testing",
recipients=["[email protected]"],
body="test")
self.assertEqual(len(outbox), 1)
msg = outbox[0]
self.assertEqual(msg.subject, "testing")
self.assertEqual(msg.recipients, ["[email protected]"])
self.assertEqual(msg.body, "test")
self.assertEqual(msg.sender, self.app.extensions['mail'].default_sender)
class TestConnection(TestCase):
def test_send_message(self):
with self.mail.record_messages() as outbox:
with self.mail.connect() as conn:
conn.send_message(subject="testing",
recipients=["[email protected]"],
body="testing")
self.assertEqual(len(outbox), 1)
sent_msg = outbox[0]
self.assertEqual(sent_msg.sender, self.app.extensions['mail'].default_sender)
def test_send_single(self):
with self.mail.record_messages() as outbox:
with self.mail.connect() as conn:
msg = Message(subject="testing",
recipients=["[email protected]"],
body="testing")
conn.send(msg)
self.assertEqual(len(outbox), 1)
sent_msg = outbox[0]
self.assertEqual(sent_msg.subject, "testing")
self.assertEqual(sent_msg.recipients, ["[email protected]"])
self.assertEqual(sent_msg.body, "testing")
self.assertEqual(sent_msg.sender, self.app.extensions['mail'].default_sender)
def test_send_many(self):
with self.mail.record_messages() as outbox:
with self.mail.connect() as conn:
for i in range(100):
msg = Message(subject="testing",
recipients=["[email protected]"],
body="testing")
conn.send(msg)
self.assertEqual(len(outbox), 100)
sent_msg = outbox[0]
self.assertEqual(sent_msg.sender, self.app.extensions['mail'].default_sender)
def test_send_without_sender(self):
self.app.extensions['mail'].default_sender = None
msg = Message(subject="testing", recipients=["[email protected]"], body="testing")
with self.mail.connect() as conn:
self.assertRaises(AssertionError, conn.send, msg)
def test_send_without_recipients(self):
msg = Message(subject="testing",
recipients=[],
body="testing")
with self.mail.connect() as conn:
self.assertRaises(AssertionError, conn.send, msg)
def test_bad_header_subject(self):
msg = Message(subject="testing\n\r",
body="testing",
recipients=["[email protected]"])
with self.mail.connect() as conn:
self.assertRaises(BadHeaderError, conn.send, msg)
def test_sendmail_with_ascii_recipient(self):
with self.mail.connect() as conn:
with mock.patch.object(conn, 'host') as host:
msg = Message(subject="testing",
sender="[email protected]",
recipients=["[email protected]"],
body="testing")
conn.send(msg)
host.sendmail.assert_called_once_with(
["[email protected]"],
msg.as_bytes() if PY3 else msg.as_string(),
msg.mail_options,
msg.rcpt_options
)
def test_sendmail_with_non_ascii_recipient(self):
with self.mail.connect() as conn:
with mock.patch.object(conn, 'host') as host:
msg = Message(subject="testing",
sender="[email protected]",
recipients=[u'ÄÜÖ → ✓ <[email protected]>'],
body="testing")
conn.send(msg)
host.sendmail.assert_called_once_with(
["=?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <[email protected]>"],
msg.as_bytes() if PY3 else msg.as_string(),
msg.mail_options,
msg.rcpt_options
)
def test_sendmail_with_ascii_body(self):
with self.mail.connect() as conn:
with mock.patch.object(conn, 'host') as host:
msg = Message(subject="testing",
sender="[email protected]",
recipients=["[email protected]"],
body="body")
conn.send(msg)
host.sendmail.assert_called_once_with(
["[email protected]"],
msg.as_bytes() if PY3 else msg.as_string(),
msg.mail_options,
msg.rcpt_options
)
def test_sendmail_with_non_ascii_body(self):
with self.mail.connect() as conn:
with mock.patch.object(conn, 'host') as host:
msg = Message(subject="testing",
sender="[email protected]",
recipients=["[email protected]"],
body=u"Öö")
conn.send(msg)
host.sendmail.assert_called_once_with(
["[email protected]"],
msg.as_bytes() if PY3 else msg.as_string(),
msg.mail_options,
msg.rcpt_options
)