Ejemplo n.º 1
0
    def test_to_header(self):
        """
        Make sure we can manually set the To header (#17444)
        """
        email = EmailMessage(
            'Subject',
            'Content',
            '*****@*****.**',
            ['*****@*****.**', '*****@*****.**'],
            headers={'To': '*****@*****.**'})
        message = email.message()
        self.assertEqual(message['To'], '*****@*****.**')
        self.assertEqual(
            email.to,
            ['*****@*****.**', '*****@*****.**'])

        # If we don't set the To header manually, it should default to the `to` argument to the constructor
        email = EmailMessage(
            'Subject', 'Content', '*****@*****.**',
            ['*****@*****.**', '*****@*****.**'])
        message = email.message()
        self.assertEqual(
            message['To'],
            '[email protected], [email protected]')
        self.assertEqual(
            email.to,
            ['*****@*****.**', '*****@*****.**'])
Ejemplo n.º 2
0
 def test_unicode_address_header(self):
     """
     Regression for #11144 - When a to/from/cc header contains unicode,
     make sure the email addresses are parsed correctly (especially with
     regards to commas)
     """
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['"Firstname Sürname" <*****@*****.**>', '*****@*****.**'])
     self.assertEqual(email.message()['To'], '=?utf-8?q?Firstname_S=C3=BCrname?= <*****@*****.**>, [email protected]')
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['"Sürname, Firstname" <*****@*****.**>', '*****@*****.**'])
     self.assertEqual(email.message()['To'], '=?utf-8?q?S=C3=BCrname=2C_Firstname?= <*****@*****.**>, [email protected]')
Ejemplo n.º 3
0
 def test_multiple_message_call(self):
     """
     Regression for #13259 - Make sure that headers are not changed when
     calling EmailMessage.message()
     """
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
     message = email.message()
     self.assertEqual(message['From'], '*****@*****.**')
     message = email.message()
     self.assertEqual(message['From'], '*****@*****.**')
Ejemplo n.º 4
0
 def test_multiple_message_call(self):
     """
     Regression for #13259 - Make sure that headers are not changed when
     calling EmailMessage.message()
     """
     email = EmailMessage('Subject',
                          'Content',
                          '*****@*****.**', ['*****@*****.**'],
                          headers={'From': '*****@*****.**'})
     message = email.message()
     self.assertEqual(message['From'], '*****@*****.**')
     message = email.message()
     self.assertEqual(message['From'], '*****@*****.**')
Ejemplo n.º 5
0
 def test_ascii(self):
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'])
     message = email.message()
     self.assertEqual(message['Subject'].encode(), 'Subject')
     self.assertEqual(message.get_payload(), 'Content')
     self.assertEqual(message['From'], '*****@*****.**')
     self.assertEqual(message['To'], '*****@*****.**')
Ejemplo n.º 6
0
 def test_multiple_recipients(self):
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**', '*****@*****.**'])
     message = email.message()
     self.assertEqual(message['Subject'].encode(), 'Subject')
     self.assertEqual(message.get_payload(), 'Content')
     self.assertEqual(message['From'], '*****@*****.**')
     self.assertEqual(message['To'], '[email protected], [email protected]')
Ejemplo n.º 7
0
    def test_encoding(self):
        """
        Regression for #12791 - Encode body correctly with other encodings
        than utf-8
        """
        email = EmailMessage('Subject', 'Firstname Sürname is a great guy.',
                             '*****@*****.**', ['*****@*****.**'])
        email.encoding = 'iso-8859-1'
        message = email.message()
        self.assertTrue(message.as_string().startswith(
            'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: [email protected]\nTo: [email protected]'
        ))
        self.assertEqual(message.get_payload(),
                         'Firstname S=FCrname is a great guy.')

        # Make sure MIME attachments also works correctly with other encodings than utf-8
        text_content = 'Firstname Sürname is a great guy.'
        html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>'
        msg = EmailMultiAlternatives('Subject', text_content,
                                     '*****@*****.**', ['*****@*****.**'])
        msg.encoding = 'iso-8859-1'
        msg.attach_alternative(html_content, "text/html")
        self.assertEqual(
            msg.message().get_payload(0).as_string(),
            'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.'
        )
        self.assertEqual(
            msg.message().get_payload(1).as_string(),
            'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>'
        )
Ejemplo n.º 8
0
 def test_space_continuation(self):
     """
     Test for space continuation character in long (ascii) subject headers (#7747)
     """
     email = EmailMessage('Long subject lines that get wrapped should use a space continuation character to get expected behavior in Outlook and Thunderbird', 'Content', '*****@*****.**', ['*****@*****.**'])
     message = email.message()
     self.assertEqual(message['Subject'], 'Long subject lines that get wrapped should use a space continuation\n character to get expected behavior in Outlook and Thunderbird')
Ejemplo n.º 9
0
 def test_from_header(self):
     """
     Make sure we can manually set the From header (#9214)
     """
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
     message = email.message()
     self.assertEqual(message['From'], '*****@*****.**')
Ejemplo n.º 10
0
 def test_dont_mangle_from_in_body(self):
     # Regression for #13433 - Make sure that EmailMessage doesn't mangle
     # 'From ' in message body.
     email = EmailMessage('Subject',
                          'From the future',
                          '*****@*****.**', ['*****@*****.**'],
                          headers={'From': '*****@*****.**'})
     self.assertFalse('>From the future' in email.message().as_string())
Ejemplo n.º 11
0
 def test_multiple_recipients(self):
     email = EmailMessage('Subject', 'Content', '*****@*****.**',
                          ['*****@*****.**', '*****@*****.**'])
     message = email.message()
     self.assertEqual(message['Subject'].encode(), 'Subject')
     self.assertEqual(message.get_payload(), 'Content')
     self.assertEqual(message['From'], '*****@*****.**')
     self.assertEqual(message['To'], '[email protected], [email protected]')
Ejemplo n.º 12
0
 def test_ascii(self):
     email = EmailMessage('Subject', 'Content', '*****@*****.**',
                          ['*****@*****.**'])
     message = email.message()
     self.assertEqual(message['Subject'].encode(), 'Subject')
     self.assertEqual(message.get_payload(), 'Content')
     self.assertEqual(message['From'], '*****@*****.**')
     self.assertEqual(message['To'], '*****@*****.**')
Ejemplo n.º 13
0
    def test_to_header(self):
        """
        Make sure we can manually set the To header (#17444)
        """
        email = EmailMessage('Subject', 'Content', '*****@*****.**',
                             ['*****@*****.**', '*****@*****.**'],
                             headers={'To': '*****@*****.**'})
        message = email.message()
        self.assertEqual(message['To'], '*****@*****.**')
        self.assertEqual(email.to, ['*****@*****.**', '*****@*****.**'])

        # If we don't set the To header manually, it should default to the `to` argument to the constructor
        email = EmailMessage('Subject', 'Content', '*****@*****.**',
                             ['*****@*****.**', '*****@*****.**'])
        message = email.message()
        self.assertEqual(message['To'], '[email protected], [email protected]')
        self.assertEqual(email.to, ['*****@*****.**', '*****@*****.**'])
Ejemplo n.º 14
0
 def test_unicode_headers(self):
     email = EmailMessage("Gżegżółka", "Content", "*****@*****.**", ["*****@*****.**"],
                          headers={"Sender": '"Firstname Sürname" <*****@*****.**>',
                                   "Comments": 'My Sürname is non-ASCII'})
     message = email.message()
     self.assertEqual(message['Subject'], '=?utf-8?b?R8W8ZWfFvMOzxYJrYQ==?=')
     self.assertEqual(message['Sender'], '=?utf-8?q?Firstname_S=C3=BCrname?= <*****@*****.**>')
     self.assertEqual(message['Comments'], '=?utf-8?q?My_S=C3=BCrname_is_non-ASCII?=')
Ejemplo n.º 15
0
 def test_message_header_overrides(self):
     """
     Specifying dates or message-ids in the extra headers overrides the
     default values (#9233)
     """
     headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
     email = EmailMessage('subject', 'content', '*****@*****.**', ['*****@*****.**'], headers=headers)
     self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: [email protected]\nTo: [email protected]\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent')
Ejemplo n.º 16
0
    def test_cc(self):
        """Regression test for Django #7722"""
        email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], cc=['*****@*****.**'])
        message = email.message()
        self.assertEqual(message['Cc'], '*****@*****.**')
        self.assertEqual(email.recipients(), ['*****@*****.**', '*****@*****.**'])

        # Test multiple CC with multiple To
        email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**', '*****@*****.**'], cc=['*****@*****.**', '*****@*****.**'])
        message = email.message()
        self.assertEqual(message['Cc'], '[email protected], [email protected]')
        self.assertEqual(email.recipients(), ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'])

        # Testing with Bcc
        email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**', '*****@*****.**'], cc=['*****@*****.**', '*****@*****.**'], bcc=['*****@*****.**'])
        message = email.message()
        self.assertEqual(message['Cc'], '[email protected], [email protected]')
        self.assertEqual(email.recipients(), ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'])
Ejemplo n.º 17
0
 def test_from_header(self):
     """
     Make sure we can manually set the From header (#9214)
     """
     email = EmailMessage('Subject',
                          'Content',
                          '*****@*****.**', ['*****@*****.**'],
                          headers={'From': '*****@*****.**'})
     message = email.message()
     self.assertEqual(message['From'], '*****@*****.**')
Ejemplo n.º 18
0
 def test_unicode_address_header(self):
     """
     Regression for #11144 - When a to/from/cc header contains unicode,
     make sure the email addresses are parsed correctly (especially with
     regards to commas)
     """
     email = EmailMessage(
         'Subject', 'Content', '*****@*****.**',
         ['"Firstname Sürname" <*****@*****.**>', '*****@*****.**'])
     self.assertEqual(
         email.message()['To'],
         '=?utf-8?q?Firstname_S=C3=BCrname?= <*****@*****.**>, [email protected]'
     )
     email = EmailMessage(
         'Subject', 'Content', '*****@*****.**',
         ['"Sürname, Firstname" <*****@*****.**>', '*****@*****.**'])
     self.assertEqual(
         email.message()['To'],
         '=?utf-8?q?S=C3=BCrname=2C_Firstname?= <*****@*****.**>, [email protected]'
     )
Ejemplo n.º 19
0
 def test_space_continuation(self):
     """
     Test for space continuation character in long (ascii) subject headers (#7747)
     """
     email = EmailMessage(
         'Long subject lines that get wrapped should use a space continuation character to get expected behavior in Outlook and Thunderbird',
         'Content', '*****@*****.**', ['*****@*****.**'])
     message = email.message()
     self.assertEqual(
         message['Subject'],
         'Long subject lines that get wrapped should use a space continuation\n character to get expected behavior in Outlook and Thunderbird'
     )
Ejemplo n.º 20
0
 def test_non_ascii_attachment_filename(self):
     """Regression test for Django #14964"""
     headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
     subject, from_email, to = 'hello', '*****@*****.**', '*****@*****.**'
     content = 'This is the message.'
     msg = EmailMessage(subject, content, from_email, [to], headers=headers)
     # Unicode in file name
     msg.attach("une pièce jointe.pdf", "%PDF-1.4.%...", mimetype="application/pdf")
     msg_str = msg.message().as_string()
     message = message_from_string(msg_str)
     payload = message.get_payload()
     self.assertEqual(payload[1].get_filename(), 'une pièce jointe.pdf')
Ejemplo n.º 21
0
 def test_recipients_as_tuple(self):
     email = EmailMessage('Subject',
                          'Content',
                          '*****@*****.**',
                          ('*****@*****.**', '*****@*****.**'),
                          cc=('*****@*****.**', '*****@*****.**'),
                          bcc=('*****@*****.**', ))
     message = email.message()
     self.assertEqual(message['Cc'], '[email protected], [email protected]')
     self.assertEqual(email.recipients(), [
         '*****@*****.**', '*****@*****.**', '*****@*****.**',
         '*****@*****.**', '*****@*****.**'
     ])
Ejemplo n.º 22
0
    def test_dont_base64_encode(self):
        # Ticket #3472
        # Shouldn't use Base64 encoding at all
        msg = EmailMessage('Subject', 'UTF-8 encoded body', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
        self.assertFalse('Content-Transfer-Encoding: base64' in msg.message().as_string())

        # Ticket #11212
        # Shouldn't use quoted printable, should detect it can represent content with 7 bit data
        msg = EmailMessage('Subject', 'Body with only ASCII characters.', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
        s = msg.message().as_string()
        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
        self.assertTrue('Content-Transfer-Encoding: 7bit' in s)

        # Shouldn't use quoted printable, should detect it can represent content with 8 bit data
        msg = EmailMessage('Subject', 'Body with latin characters: àáä.', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
        s = msg.message().as_string()
        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
        self.assertTrue('Content-Transfer-Encoding: 8bit' in s)

        msg = EmailMessage('Subject', 'Body with non latin characters: А Б В Г Д Е Ж Ѕ З И І К Л М Н О П.', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
        s = msg.message().as_string()
        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
        self.assertTrue('Content-Transfer-Encoding: 8bit' in s)
Ejemplo n.º 23
0
    def test_dont_base64_encode(self):
        # Ticket #3472
        # Shouldn't use Base64 encoding at all
        msg = EmailMessage('Subject',
                           'UTF-8 encoded body',
                           '*****@*****.**', ['*****@*****.**'],
                           headers={'From': '*****@*****.**'})
        self.assertFalse(
            'Content-Transfer-Encoding: base64' in msg.message().as_string())

        # Ticket #11212
        # Shouldn't use quoted printable, should detect it can represent content with 7 bit data
        msg = EmailMessage('Subject',
                           'Body with only ASCII characters.',
                           '*****@*****.**', ['*****@*****.**'],
                           headers={'From': '*****@*****.**'})
        s = msg.message().as_string()
        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
        self.assertTrue('Content-Transfer-Encoding: 7bit' in s)

        # Shouldn't use quoted printable, should detect it can represent content with 8 bit data
        msg = EmailMessage('Subject',
                           'Body with latin characters: àáä.',
                           '*****@*****.**', ['*****@*****.**'],
                           headers={'From': '*****@*****.**'})
        s = msg.message().as_string()
        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
        self.assertTrue('Content-Transfer-Encoding: 8bit' in s)

        msg = EmailMessage(
            'Subject',
            u'Body with non latin characters: А Б В Г Д Е Ж Ѕ З И І К Л М Н О П.',
            '*****@*****.**', ['*****@*****.**'],
            headers={'From': '*****@*****.**'})
        s = msg.message().as_string()
        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
        self.assertTrue('Content-Transfer-Encoding: 8bit' in s)
Ejemplo n.º 24
0
    def test_cc(self):
        """Regression test for Django #7722"""
        email = EmailMessage('Subject',
                             'Content',
                             '*****@*****.**', ['*****@*****.**'],
                             cc=['*****@*****.**'])
        message = email.message()
        self.assertEqual(message['Cc'], '*****@*****.**')
        self.assertEqual(email.recipients(),
                         ['*****@*****.**', '*****@*****.**'])

        # Test multiple CC with multiple To
        email = EmailMessage('Subject',
                             'Content',
                             '*****@*****.**',
                             ['*****@*****.**', '*****@*****.**'],
                             cc=['*****@*****.**', '*****@*****.**'])
        message = email.message()
        self.assertEqual(message['Cc'], '[email protected], [email protected]')
        self.assertEqual(email.recipients(), [
            '*****@*****.**', '*****@*****.**', '*****@*****.**',
            '*****@*****.**'
        ])

        # Testing with Bcc
        email = EmailMessage('Subject',
                             'Content',
                             '*****@*****.**',
                             ['*****@*****.**', '*****@*****.**'],
                             cc=['*****@*****.**', '*****@*****.**'],
                             bcc=['*****@*****.**'])
        message = email.message()
        self.assertEqual(message['Cc'], '[email protected], [email protected]')
        self.assertEqual(email.recipients(), [
            '*****@*****.**', '*****@*****.**', '*****@*****.**',
            '*****@*****.**', '*****@*****.**'
        ])
Ejemplo n.º 25
0
 def test_non_ascii_attachment_filename(self):
     """Regression test for Django #14964"""
     headers = {
         "Date": "Fri, 09 Nov 2001 01:08:47 -0000",
         "Message-ID": "foo"
     }
     subject, from_email, to = 'hello', '*****@*****.**', '*****@*****.**'
     content = 'This is the message.'
     msg = EmailMessage(subject, content, from_email, [to], headers=headers)
     # Unicode in file name
     msg.attach(u"une pièce jointe.pdf",
                "%PDF-1.4.%...",
                mimetype="application/pdf")
     msg_str = msg.message().as_string()
     message = message_from_string(msg_str)
     payload = message.get_payload()
     self.assertEqual(payload[1].get_filename(), u'une pièce jointe.pdf')
Ejemplo n.º 26
0
 def test_unicode_headers(self):
     email = EmailMessage(u"Gżegżółka",
                          "Content",
                          "*****@*****.**", ["*****@*****.**"],
                          headers={
                              "Sender":
                              '"Firstname Sürname" <*****@*****.**>',
                              "Comments": 'My Sürname is non-ASCII'
                          })
     message = email.message()
     self.assertEqual(message['Subject'],
                      '=?utf-8?b?R8W8ZWfFvMOzxYJrYQ==?=')
     self.assertEqual(
         message['Sender'],
         '=?utf-8?q?Firstname_S=C3=BCrname?= <*****@*****.**>')
     self.assertEqual(message['Comments'],
                      '=?utf-8?q?My_S=C3=BCrname_is_non-ASCII?=')
Ejemplo n.º 27
0
 def test_message_header_overrides(self):
     """
     Specifying dates or message-ids in the extra headers overrides the
     default values (#9233)
     """
     headers = {
         "date": "Fri, 09 Nov 2001 01:08:47 -0000",
         "Message-ID": "foo"
     }
     email = EmailMessage('subject',
                          'content',
                          '*****@*****.**', ['*****@*****.**'],
                          headers=headers)
     self.assertEqual(
         email.message().as_string(),
         'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: [email protected]\nTo: [email protected]\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent'
     )
Ejemplo n.º 28
0
    def test_encoding(self):
        """
        Regression for #12791 - Encode body correctly with other encodings
        than utf-8
        """
        email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', '*****@*****.**', ['*****@*****.**'])
        email.encoding = 'iso-8859-1'
        message = email.message()
        self.assertTrue(message.as_string().startswith('Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: [email protected]\nTo: [email protected]'))
        self.assertEqual(message.get_payload(), 'Firstname S=FCrname is a great guy.')

        # Make sure MIME attachments also works correctly with other encodings than utf-8
        text_content = 'Firstname Sürname is a great guy.'
        html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>'
        msg = EmailMultiAlternatives('Subject', text_content, '*****@*****.**', ['*****@*****.**'])
        msg.encoding = 'iso-8859-1'
        msg.attach_alternative(html_content, "text/html")
        self.assertEqual(msg.message().get_payload(0).as_string(), 'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.')
        self.assertEqual(msg.message().get_payload(1).as_string(), 'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>')
Ejemplo n.º 29
0
 def test_dont_mangle_from_in_body(self):
     # Regression for #13433 - Make sure that EmailMessage doesn't mangle
     # 'From ' in message body.
     email = EmailMessage('Subject', 'From the future', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
     self.assertFalse('>From the future' in email.message().as_string())
Ejemplo n.º 30
0
 def test_recipients_as_tuple(self):
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ('*****@*****.**', '*****@*****.**'), cc=('*****@*****.**', '*****@*****.**'), bcc=('*****@*****.**',))
     message = email.message()
     self.assertEqual(message['Cc'], '[email protected], [email protected]')
     self.assertEqual(email.recipients(), ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'])