Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ def get_payload(self, i=None, decode=False):
return payload
if isinstance(payload, str):
try:
bpayload = payload.encode('ascii', 'surrogateescape')
if cte == '8bit':
bpayload = payload.encode(self.get_param('charset', 'ascii'), 'surrogateescape')
else:
bpayload = payload.encode('ascii', 'surrogateescape')
except UnicodeEncodeError:
# This won't happen for RFC compliant messages (messages
# containing only ASCII code points in the unicode input).
Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def test_broken_unicode_payload(self):
x = 'this is a br\xf6ken thing to do'
msg = Message()
msg['content-type'] = 'text/plain'
msg['content-transfer-encoding'] = '8bit'
msg['content-transfer-encoding'] = '7bit'
msg.set_payload(x)
self.assertEqual(msg.get_payload(decode=True),
bytes(x, 'raw-unicode-escape'))
Expand Down Expand Up @@ -750,6 +750,15 @@ def test_binary_base64_payload(self):
b'foo\xe6\x96\x87bar',
'get_payload returns wrong result with charset %s.' % charset)

def test_8bit_utf8_payload(self):
x = 'this is the hötel'
msg = Message()
msg['content-type'] = 'text/plain'
msg['content-transfer-encoding'] = '8bit'
msg.set_payload(x)
self.assertEqual(msg.get_payload(decode=True),
bytes(x, 'utf-8'))

def test_binary_uuencode_payload(self):
for charset in ('latin-1', 'ascii'):
for encoding in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Attempt to decode emails with UTF-8 when the CTE is ``8bit``.
Loading