Fixed #29830 -- Fixed loss of custom utf-8 body encoding in mails.

This commit is contained in:
jannschu 2018-10-22 12:21:33 -07:00 committed by Tim Graham
parent 043407ec7e
commit efc0f77f02
2 changed files with 17 additions and 2 deletions

View File

@ -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()

View File

@ -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.