Exemplo n.º 1
0
    def test_plain_message_with_ascii_attachment(self):
        msg = Message(subject="subject",
                      recipients=["*****@*****.**"],
                      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\n', msg.as_string())
Exemplo n.º 2
0
    def test_plain_message_with_attachments(self):
        msg = Message(sender="*****@*****.**",
                      subject="subject",
                      recipients=["*****@*****.**"],
                      body="hello")

        msg.attach(data=b"this is a test",
                   content_type="text/plain")

        self.assertIn('Content-Type: multipart/mixed', msg.as_string())
Exemplo n.º 3
0
 def test_attach(self):
     msg = Message(subject="testing",
                   recipients=["*****@*****.**"],
                   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")
Exemplo n.º 4
0
    def test_plain_message_with_unicode_attachment(self):
        msg = Message(subject="subject",
                      recipients=["*****@*****.**"],
                      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'
            ])
Exemplo n.º 5
0
    def test_html_message_with_attachments(self):
        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")

        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')
Exemplo n.º 6
0
    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())