def test_cc(self): """Regression test for #7722""" email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], cc=['*****@*****.**']) message = email.message() self.assertEqual(message['Cc'], '*****@*****.**') self.assertEqual(email.recipients(), ['*****@*****.**', '*****@*****.**']) # Verify headers old_stdout = sys.stdout sys.stdout = StringIO() connection = console.EmailBackend() connection.send_messages([email]) self.assertTrue(sys.stdout.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: [email protected]\nTo: [email protected]\nCc: [email protected]\nDate: ')) sys.stdout = old_stdout # 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(), ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'])
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'], '*****@*****.**')
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]')
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"], "*****@*****.**")
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'], '*****@*****.**')
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]')
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())
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')
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]")
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"], "*****@*****.**")
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>', )
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.assertMessageHasHeaders(message, { ('MIME-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('Content-Transfer-Encoding', 'quoted-printable'), ('Subject', 'Subject'), ('From', '*****@*****.**'), ('To', '*****@*****.**')}) 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") payload0 = msg.message().get_payload(0) self.assertMessageHasHeaders(payload0, { ('MIME-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('Content-Transfer-Encoding', 'quoted-printable')}) self.assertTrue(payload0.as_string().endswith('\n\nFirstname S=FCrname is a great guy.')) payload1 = msg.message().get_payload(1) self.assertMessageHasHeaders(payload1, { ('MIME-Version', '1.0'), ('Content-Type', 'text/html; charset="iso-8859-1"'), ('Content-Transfer-Encoding', 'quoted-printable')}) self.assertTrue(payload1.as_string().endswith('\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>'))
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'], '*****@*****.**')
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(), b'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')
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?=')
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 contain a space continuation character to get expected behavior in Outlook and Thunderbird', 'Content', '*****@*****.**', ['*****@*****.**']) message = email.message() # Note that in Python 3, maximum line length has increased from 76 to 78 self.assertEqual(message['Subject'].encode(), b'Long subject lines that get wrapped should contain a space continuation\n character to get expected behavior in Outlook and Thunderbird')
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, ['*****@*****.**', '*****@*****.**'])
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"], "*****@*****.**")
def test_cc(self): """Regression test for #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(), ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'])
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", )
def test_cc(self): """Regression test for #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(), ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"], )
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(), ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"], )
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?=')
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' )
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(sorted(email.message().items()), [ ('Content-Transfer-Encoding', '7bit'), ('Content-Type', 'text/plain; charset="utf-8"'), ('From', '*****@*****.**'), ('MIME-Version', '1.0'), ('Message-ID', 'foo'), ('Subject', 'subject'), ('To', '*****@*****.**'), ('date', 'Fri, 09 Nov 2001 01:08:47 -0000'), ])
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.assertMessageHasHeaders(email.message(), { ('Content-Transfer-Encoding', '7bit'), ('Content-Type', 'text/plain; charset="utf-8"'), ('From', '*****@*****.**'), ('MIME-Version', '1.0'), ('Message-ID', 'foo'), ('Subject', 'subject'), ('To', '*****@*****.**'), ('date', 'Fri, 09 Nov 2001 01:08:47 -0000'), })
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>')
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.assertMessageHasHeaders( message, {('MIME-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('Content-Transfer-Encoding', 'quoted-printable'), ('Subject', 'Subject'), ('From', '*****@*****.**'), ('To', '*****@*****.**')}) 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") payload0 = msg.message().get_payload(0) self.assertMessageHasHeaders( payload0, {('MIME-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('Content-Transfer-Encoding', 'quoted-printable')}) self.assertTrue(payload0.as_string().endswith( '\n\nFirstname S=FCrname is a great guy.')) payload1 = msg.message().get_payload(1) self.assertMessageHasHeaders( payload1, {('MIME-Version', '1.0'), ('Content-Type', 'text/html; charset="iso-8859-1"'), ('Content-Transfer-Encoding', 'quoted-printable')}) self.assertTrue(payload1.as_string().endswith( '\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>'))
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())
def main(): print("main") message()
def data(): # here we want to get the value of user (i.e. ?user=some-value) data = request.args.get('data') email.message(data)
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(), ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'])
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(b'>From the future' in email.message().as_string())