Skip to content

[3.8] bpo-27321 Fix email.generator.py to not replace a non-existent header. (GH-18074) #22797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2020
Merged
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 @@ -186,7 +186,11 @@ def _write(self, msg):
# If we munged the cte, copy the message again and re-fix the CTE.
if munge_cte:
msg = deepcopy(msg)
msg.replace_header('content-transfer-encoding', munge_cte[0])
# Preserve the header order if the CTE header already exists.
if msg.get('content-transfer-encoding') is None:
msg['Content-Transfer-Encoding'] = munge_cte[0]
else:
msg.replace_header('content-transfer-encoding', munge_cte[0])
msg.replace_header('content-type', munge_cte[1])
# Write the headers. First we see if the message object wants to
# handle that itself. If not, we'll do it generically.
Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,41 @@ def test_as_string_policy(self):
g.flatten(msg)
self.assertEqual(fullrepr, s.getvalue())

def test_nonascii_as_string_without_cte(self):
m = textwrap.dedent("""\
MIME-Version: 1.0
Content-type: text/plain; charset="iso-8859-1"

Test if non-ascii messages with no Content-Transfer-Encoding set
can be as_string'd:
Föö bär
""")
source = m.encode('iso-8859-1')
expected = textwrap.dedent("""\
MIME-Version: 1.0
Content-type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Test if non-ascii messages with no Content-Transfer-Encoding set
can be as_string'd:
F=F6=F6 b=E4r
""")
msg = email.message_from_bytes(source)
self.assertEqual(msg.as_string(), expected)

def test_nonascii_as_string_without_content_type_and_cte(self):
m = textwrap.dedent("""\
MIME-Version: 1.0

Test if non-ascii messages with no Content-Type nor
Content-Transfer-Encoding set can be as_string'd:
Föö bär
""")
source = m.encode('iso-8859-1')
expected = source.decode('ascii', 'replace')
msg = email.message_from_bytes(source)
self.assertEqual(msg.as_string(), expected)

def test_as_bytes(self):
msg = self._msgobj('msg_01.txt')
with openfile('msg_01.txt') as fp:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed KeyError exception when flattening an email to a string attempts to
replace a non-existent Content-Transfer-Encoding header.