diff --git a/django/core/mail/message.py b/django/core/mail/message.py index 9a0f0eba45..2f89ffbfc4 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -170,7 +170,7 @@ class SafeMIMEText(MIMEMixin, MIMEText): MIMEText.__setitem__(self, name, val) def set_payload(self, payload, charset=None): - if charset == 'utf-8': + if charset == 'utf-8' and not isinstance(charset, Charset.Charset): has_long_lines = any( len(l.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT for l in payload.splitlines() diff --git a/tests/mail/tests.py b/tests/mail/tests.py index b2de5e4c10..ac44996ff9 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -8,7 +8,7 @@ import socket import sys import tempfile import threading -from email import message_from_binary_file, message_from_bytes +from email import charset, message_from_binary_file, message_from_bytes from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr @@ -686,6 +686,21 @@ class MailTests(HeadersCheckMixin, SimpleTestCase): # The child message header is not base64 encoded self.assertIn('Child Subject', parent_s) + def test_custom_utf8_encoding(self): + """A UTF-8 charset with a custom body encoding is respected.""" + body = 'Body with latin characters: àáä.' + msg = EmailMessage('Subject', body, 'bounce@example.com', ['to@example.com']) + encoding = charset.Charset('utf-8') + encoding.body_encoding = charset.QP + msg.encoding = encoding + message = msg.message() + self.assertMessageHasHeaders(message, { + ('MIME-Version', '1.0'), + ('Content-Type', 'text/plain; charset="utf-8"'), + ('Content-Transfer-Encoding', 'quoted-printable'), + }) + self.assertEqual(message.get_payload(), encoding.body_encode(body)) + def test_sanitize_address(self): """ Email addresses are properly sanitized.