def test_message_charset(self): msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], 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="*****@*****.**", subject="subject", recipients=["*****@*****.**"], 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="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) 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="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) 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="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) 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"送信者", "*****@*****.**"), subject=u"表題", recipients=["*****@*****.**"], reply_to=u"返信先 <*****@*****.**>", 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="*****@*****.**", subject=u"表題", recipients=["*****@*****.**"], 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="*****@*****.**", subject="subject", recipients=["*****@*****.**"], charset='utf-8') msg.body = u'内容' self.assertIn('Subject: =?utf-8?', msg.as_string()) self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string()) # ascii subject msg = Message(sender="*****@*****.**", subject="subject", recipients=["*****@*****.**"], 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="*****@*****.**", subject="subject", recipients=["*****@*****.**"]) msg.body = "normal ascii text" self.assertNotIn('Subject: =?', msg.as_string()) self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())