From dd001848920713fe616c03db6946762e8aa7c4e3 Mon Sep 17 00:00:00 2001 From: kalombo Date: Fri, 7 Apr 2017 15:23:25 +0500 Subject: [PATCH] Fixed #28042 -- Fixed crash when using a two-tuple in EmailMessage's attachments arg. --- django/core/mail/message.py | 8 +++++++- docs/releases/1.11.1.txt | 3 +++ tests/mail/tests.py | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index f12531af4c8..51023768e62 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -236,7 +236,13 @@ class EmailMessage: 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 diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index d1e054bb188..a10f3f47efa 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -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`). diff --git a/tests/mail/tests.py b/tests/mail/tests.py index 05fe61026fe..50922e21d7d 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -374,6 +374,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])