Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -417,7 +417,11 @@ class BytesGenerator(Generator):
"""

def write(self, s):
self._fp.write(s.encode('ascii', 'surrogateescape'))
if getattr(self.policy, "utf8", False):
encoded = s.encode('utf-8', 'surrogateescape')
else:
encoded = s.encode('ascii', 'surrogateescape')
self._fp.write(encoded)

def _new_buffer(self):
return BytesIO()
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_email/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,20 @@ def test_smtp_policy(self):
g = BytesGenerator(s, policy=policy.SMTP)
g.flatten(msg)
self.assertEqual(s.getvalue(), expected)
def test_utf8_round_trip_preserves_unicode_body(self):
msg = EmailMessage()
msg['From'] = "Páolo <fő[email protected]>"
msg['To'] = 'Dinsdale'
msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
msg.set_content("oh là là, know what I mean, know what I mean?")
s = io.BytesIO()
g = BytesGenerator(s, policy=policy.SMTPUTF8)
g.flatten(msg)
out = s.getvalue()
parsed = message_from_bytes(out, policy=policy.default.clone(utf8=True))

self.assertEqual(parsed["Subject"], 'Nudge nudge, wink, wink \u1F609')
self.assertEqual(parsed.get_body().get_content(), "oh là là, know what I mean, know what I mean?\r\n")


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug where emails with ``multipart/related`` content did not round-trip
correctly through ``email.parser.parsebytes()`` and ``as_bytes()``.
Loading