def test_plain_message_with_attachments(): msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], body="hello") msg.attach(data=b"this is a test", content_type="text/plain") assert 'Content-Type: multipart/mixed' in msg.as_string()
def test_plain_message_with_ascii_converted_attachment(): msg = Message(subject="subject", recipients=["*****@*****.**"], body="hello", sender="*****@*****.**", ascii_attachments=True) msg.attach(data=b"this is a test", content_type="text/plain", filename='ünicödeß ←.→ ✓.txt') assert 'Content-Disposition: attachment; filename="unicode . .txt"' in msg.as_string( )
def test_plain_message_with_unicode_attachment(): msg = Message( subject="subject", recipients=["*****@*****.**"], body="hello", sender="*****@*****.**", ) msg.attach(data=b"this is a test", content_type="text/plain", filename='ünicöde ←→ ✓.txt') parsed = email.message_from_string(msg.as_string()) assert re.sub( r'\s+', ' ', parsed.get_payload()[1].get('Content-Disposition') ) in [ '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_html_message_with_attachments(): html_text = "<p>Hello World</p>" plain_text = 'Hello World' msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], body=plain_text, html=html_text) msg.attach(data=b"this is a test", content_type="text/plain") assert html_text == msg.html assert 'Content-Type: multipart/alternative' in msg.as_string() parsed = email.message_from_string(msg.as_string()) assert len(parsed.get_payload()) == 2 body, attachment = parsed.get_payload() assert len(body.get_payload()) == 2 plain, html = body.get_payload() assert plain.get_payload() == plain_text assert html.get_payload() == html_text assert base64.b64decode(attachment.get_payload()), b'this is a test'
def test_message_charset(): msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], charset='us-ascii') # ascii body msg.body = "normal ascii text" assert 'Content-Type: text/plain; charset="us-ascii"' in msg.as_string() # ascii html msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], charset='us-ascii') msg.body = None msg.html = "<html><h1>hello</h1></html>" assert 'Content-Type: text/html; charset="us-ascii"' in msg.as_string() # unicode body msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) msg.body = "ünicöde ←→ ✓" assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string() # unicode body and unicode html msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) msg.html = "ünicöde ←→ ✓" assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string() assert 'Content-Type: text/html; charset="utf-8"' in msg.as_string() # unicode body and attachments msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) msg.html = None msg.attach(data=b"foobar", content_type='text/csv', headers={'X-Extra-Header': 'Yes'}) assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string() assert 'X-Extra-Header' in msg.as_string() # unicode sender as tuple msg = Message(sender=("送信者", "*****@*****.**"), subject="表題", recipients=["*****@*****.**"], reply_to="返信先 <*****@*****.**>", charset='shift_jis') # japanese msg.body = '内容' assert 'From: =?iso-2022-jp?' in msg.as_string() assert 'From: =?utf-8?' not in msg.as_string() assert 'Subject: =?iso-2022-jp?' in msg.as_string() assert 'Subject: =?utf-8?' not in msg.as_string() assert 'Reply-To: =?iso-2022-jp?' in msg.as_string() assert 'Reply-To: =?utf-8?' not in msg.as_string() assert 'Content-Type: text/plain; charset="iso-2022-jp"' in msg.as_string() # unicode subject sjis msg = Message(sender="*****@*****.**", subject="表題", recipients=["*****@*****.**"], charset='shift_jis') # japanese msg.body = '内容' assert 'Subject: =?iso-2022-jp?' in msg.as_string() assert 'Content-Type: text/plain; charset="iso-2022-jp"', msg.as_string() # unicode subject utf-8 msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], charset='utf-8') msg.body = '内容' assert 'Subject: subject' in msg.as_string() assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string() # ascii subject msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], charset='us-ascii') msg.body = "normal ascii text" assert 'Subject: =?us-ascii?' not in msg.as_string() assert 'Content-Type: text/plain; charset="us-ascii"' in msg.as_string() # default charset msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) msg.body = "normal ascii text" assert 'Subject: =?' not in msg.as_string() assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string()