Skip to content
Closed
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
6 changes: 5 additions & 1 deletion Lib/email/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ class BytesGenerator(Generator):
"""

def write(self, s):
self._fp.write(s.encode('ascii', 'surrogateescape'))
try:
s = s.encode('ascii', 'surrogateescape')
except UnicodeEncodeError:
s = s.encode('ascii', 'replace')
self._fp.write(s)

def _new_buffer(self):
return BytesIO()
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_email/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ def test_cte_type_7bit_transforms_8bit_cte(self):
g.flatten(msg)
self.assertEqual(s.getvalue(), expected)

def test_flatten_charset_utf8_with_nonascii(self):
source = textwrap.dedent("""\
Subject: Defective email
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

I think thatâ**s the way to go.
""")
expected = source.encode('ascii', 'replace')
msg = message_from_string(source)
s = io.BytesIO()
g = BytesGenerator(s)
g.flatten(msg)
self.assertEqual(s.getvalue(), expected)

def test_smtputf8_policy(self):
msg = EmailMessage()
msg['From'] = "Páolo <főo@bar.com>"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue in email.generator.BytesGenerator.flatten() which would throw
UnicodeEncodeError with a message with a non-ascii body .