Beispiel #1
0
 def test_extra_headers(self):
     msg = Message(sender="*****@*****.**",
                   subject="subject",
                   recipients=["*****@*****.**"],
                   body="hello",
                   extra_headers={'X-Extra-Header': 'Yes'})
     self.assertIn('X-Extra-Header: Yes', msg.as_string())
Beispiel #2
0
    def test_bad_header_sender(self):
        msg = Message(subject="testing",
                      sender="[email protected]\n\r",
                      recipients=["*****@*****.**"],
                      body="testing")

        self.assertIn('From: [email protected]', msg.as_string())
Beispiel #3
0
 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('*****@*****.**', msg.as_string())
     self.assertIn('*****@*****.**', msg.as_string())
     self.assertIn('*****@*****.**', msg.as_string())
Beispiel #4
0
 def test_plain_message(self):
     plain_text = "Hello Joe,\nHow are you?"
     msg = Message(sender="*****@*****.**",
                   subject="subject",
                   recipients=["*****@*****.**"],
                   body=plain_text)
     self.assertEqual(plain_text, msg.body)
     self.assertIn('Content-Type: text/plain', msg.as_string())
Beispiel #5
0
 def test_cc(self):
     msg = Message(sender="*****@*****.**",
                   subject="testing",
                   recipients=["*****@*****.**"],
                   body="testing",
                   cc=["*****@*****.**"])
     response = msg.as_string()
     self.assertIn("Cc: [email protected]", str(response))
Beispiel #6
0
    def test_bad_header_recipient(self):
        msg = Message(subject="testing",
                      sender="*****@*****.**",
                      recipients=[
                          "*****@*****.**",
                          "to\r\[email protected]"],
                      body="testing")

        self.assertIn('To: [email protected]\n', msg.as_string())
Beispiel #7
0
    def test_html_message(self):
        html_text = "<p>Hello World</p>"
        msg = Message(sender="*****@*****.**",
                      subject="subject",
                      recipients=["*****@*****.**"],
                      html=html_text)

        self.assertEqual(html_text, msg.html)
        self.assertIn('Content-Type: multipart/alternative', msg.as_string())
Beispiel #8
0
    def test_reply_to(self):
        msg = Message(subject="testing",
                      recipients=["*****@*****.**"],
                      sender="spammer <*****@*****.**>",
                      reply_to="somebody <*****@*****.**>",
                      body="testing")
        response = msg.as_string()

        h = Header("Reply-To: %s" % sanitize_address('somebody <*****@*****.**>'))
        self.assertIn(h.encode(), str(response))
Beispiel #9
0
    def test_msgid_header(self):
        msg = Message(sender="*****@*****.**",
                      subject="subject",
                      recipients=["*****@*****.**"],
                      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())
Beispiel #10
0
    def test_bad_header_reply_to(self):
        msg = Message(subject="testing",
                      sender="*****@*****.**",
                      reply_to="[email protected]\n\r",
                      recipients=["*****@*****.**"],
                      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())
Beispiel #11
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())
Beispiel #12
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())
Beispiel #13
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")
Beispiel #14
0
    def test_date_header(self):
        before = time.time()
        msg = Message(sender="*****@*****.**",
                      subject="subject",
                      recipients=["*****@*****.**"],
                      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())
Beispiel #15
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'
            ])
Beispiel #16
0
    def test_unicode_headers(self):
        msg = Message(subject="subject",
                      sender=u'ÄÜÖ → ✓ <*****@*****.**>',
                      recipients=[u"Ä <*****@*****.**>", u"Ü <*****@*****.**>"],
                      cc=[u"Ö <*****@*****.**>"])

        response = msg.as_string()
        a1 = sanitize_address(u"Ä <*****@*****.**>")
        a2 = sanitize_address(u"Ü <*****@*****.**>")
        h1_a = Header("To: %s, %s" % (a1, a2))
        h1_b = Header("To: %s, %s" % (a2, a1))
        h2 = Header("From: %s" % sanitize_address(u"ÄÜÖ → ✓ <*****@*****.**>"))
        h3 = Header("Cc: %s" % sanitize_address(u"Ö <*****@*****.**>"))

        # 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)
Beispiel #17
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')
Beispiel #18
0
 def test_add_recipient(self):
     msg = Message("testing")
     msg.add_recipient("*****@*****.**")
     self.assertEqual(msg.recipients, ["*****@*****.**"])
Beispiel #19
0
 def test_sendto_properly_set(self):
     msg = Message(subject="subject", recipients=["*****@*****.**"],
                   cc=["*****@*****.**"], bcc=["*****@*****.**"])
     self.assertEqual(len(msg.send_to), 3)
     msg.add_recipient("*****@*****.**")
     self.assertEqual(len(msg.send_to), 3)
Beispiel #20
0
 def test_recipients_properly_initialized(self):
     msg = Message(subject="subject")
     self.assertEqual(msg.recipients, [])
     msg2 = Message(subject="subject")
     msg2.add_recipient("*****@*****.**")
     self.assertEqual(len(msg2.recipients), 1)
Beispiel #21
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())
Beispiel #22
0
    def test_unicode_sender(self):
        msg = Message(subject="subject",
                      sender=u'ÄÜÖ → ✓ <*****@*****.**>>',
                      recipients=["*****@*****.**"])

        self.assertIn('From: =?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <*****@*****.**>', msg.as_string())
Beispiel #23
0
 def test_message_str(self):
     msg = Message(sender="*****@*****.**",
                   subject="subject",
                   recipients=["*****@*****.**"],
                   body="some plain text")
     self.assertEqual(msg.as_string(), str(msg))