[1.11.x] Fixed #28042 -- Fixed crash when using a two-tuple in EmailMessage's attachments arg.

Backport of dd00184892 from master
This commit is contained in:
kalombo 2017-04-07 15:23:25 +05:00 committed by Tim Graham
parent a95eba7d98
commit 734e6bb2b8
3 changed files with 17 additions and 1 deletions

View File

@ -286,7 +286,13 @@ class EmailMessage(object):
self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
self.subject = subject
self.body = body
self.attachments = attachments or []
self.attachments = []
if attachments:
for attachment in attachments:
if isinstance(attachment, MIMEBase):
self.attach(attachment)
else:
self.attach(*attachment)
self.extra_headers = headers or {}
self.connection = connection

View File

@ -15,3 +15,6 @@ Bugfixes
* Fixed a crash when using a ``__icontains`` lookup on a ``ArrayField``
(:ticket:`28038`).
* Fixed a crash when using a two-tuple in ``EmailMessage``s ``attachments``
argument (:ticket:`28042`).

View File

@ -387,6 +387,13 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
self.assertEqual(payload[0].get_content_type(), 'multipart/alternative')
self.assertEqual(payload[1].get_content_type(), 'application/pdf')
def test_attachments_two_tuple(self):
msg = EmailMessage(attachments=[('filename1', 'content1')])
filename, content, mimetype = self.get_decoded_attachments(msg)[0]
self.assertEqual(filename, 'filename1')
self.assertEqual(content, b'content1')
self.assertEqual(mimetype, 'application/octet-stream')
def test_attachments_MIMEText(self):
txt = MIMEText('content1')
msg = EmailMessage(attachments=[txt])