From af35c69a3c595ff77188ff0d7dac6ab747484ac3 Mon Sep 17 00:00:00 2001 From: Igor Tokarev Date: Mon, 28 Aug 2017 09:28:18 +0500 Subject: [PATCH] Fixed #26344 -- Made EmailMessage include alternatives when the body is empty and it has attachments. --- django/core/mail/message.py | 2 +- tests/mail/tests.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index eb10ff1b73..9c68b8be6d 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -354,7 +354,7 @@ class EmailMessage: encoding = self.encoding or settings.DEFAULT_CHARSET body_msg = msg msg = SafeMIMEMultipart(_subtype=self.mixed_subtype, encoding=encoding) - if self.body: + if self.body or body_msg.is_multipart(): msg.attach(body_msg) for attachment in self.attachments: if isinstance(attachment, MIMEBase): diff --git a/tests/mail/tests.py b/tests/mail/tests.py index e1a7feb1f9..b759484e04 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -319,6 +319,17 @@ class MailTests(HeadersCheckMixin, SimpleTestCase): self.assertEqual(msg.message()['To'], '=?iso-8859-1?q?S=FCrname=2C_Firstname?= ') self.assertEqual(msg.message()['Subject'], '=?iso-8859-1?q?Message_from_Firstname_S=FCrname?=') + def test_safe_mime_multipart_with_attachments(self): + """ + EmailMultiAlternatives includes alternatives if the body is empty and + it has attachments. + """ + msg = EmailMultiAlternatives(body='') + html_content = '

This is html

' + msg.attach_alternative(html_content, 'text/html') + msg.attach('example.txt', 'Text file content', 'text/plain') + self.assertIn(html_content, msg.message().as_string()) + def test_encoding(self): """ Regression for #12791 - Encode body correctly with other encodings