Skip to content

Commit 72ce82a

Browse files
bpo-27321 Fix email.generator.py to not replace a non-existent header. (GH-18074)
This PR replaces GH-1977. The reason for the replacement is two-fold. The fix itself is different is that if the CTE header doesn't exist in the original message, it is inserted. This is important because the new CTE could be quoted-printable whereas the original is implicit 8bit. Also the tests are different. The test_nonascii_as_string_without_cte test in GH-1977 doesn't actually test the issue in that it passes without the fix. The test_nonascii_as_string_without_content_type_and_cte test is improved here, and even though it doesn't fail without the fix, it is included for completeness. Automerge-Triggered-By: @warsaw (cherry picked from commit bf83822) Co-authored-by: Mark Sapiro <[email protected]>
1 parent 335eb57 commit 72ce82a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Lib/email/generator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ def _write(self, msg):
186186
# If we munged the cte, copy the message again and re-fix the CTE.
187187
if munge_cte:
188188
msg = deepcopy(msg)
189-
msg.replace_header('content-transfer-encoding', munge_cte[0])
189+
# Preserve the header order if the CTE header already exists.
190+
if msg.get('content-transfer-encoding') is None:
191+
msg['Content-Transfer-Encoding'] = munge_cte[0]
192+
else:
193+
msg.replace_header('content-transfer-encoding', munge_cte[0])
190194
msg.replace_header('content-type', munge_cte[1])
191195
# Write the headers. First we see if the message object wants to
192196
# handle that itself. If not, we'll do it generically.

Lib/test/test_email/test_email.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,41 @@ def test_as_string_policy(self):
311311
g.flatten(msg)
312312
self.assertEqual(fullrepr, s.getvalue())
313313

314+
def test_nonascii_as_string_without_cte(self):
315+
m = textwrap.dedent("""\
316+
MIME-Version: 1.0
317+
Content-type: text/plain; charset="iso-8859-1"
318+
319+
Test if non-ascii messages with no Content-Transfer-Encoding set
320+
can be as_string'd:
321+
Föö bär
322+
""")
323+
source = m.encode('iso-8859-1')
324+
expected = textwrap.dedent("""\
325+
MIME-Version: 1.0
326+
Content-type: text/plain; charset="iso-8859-1"
327+
Content-Transfer-Encoding: quoted-printable
328+
329+
Test if non-ascii messages with no Content-Transfer-Encoding set
330+
can be as_string'd:
331+
F=F6=F6 b=E4r
332+
""")
333+
msg = email.message_from_bytes(source)
334+
self.assertEqual(msg.as_string(), expected)
335+
336+
def test_nonascii_as_string_without_content_type_and_cte(self):
337+
m = textwrap.dedent("""\
338+
MIME-Version: 1.0
339+
340+
Test if non-ascii messages with no Content-Type nor
341+
Content-Transfer-Encoding set can be as_string'd:
342+
Föö bär
343+
""")
344+
source = m.encode('iso-8859-1')
345+
expected = source.decode('ascii', 'replace')
346+
msg = email.message_from_bytes(source)
347+
self.assertEqual(msg.as_string(), expected)
348+
314349
def test_as_bytes(self):
315350
msg = self._msgobj('msg_01.txt')
316351
with openfile('msg_01.txt') as fp:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed KeyError exception when flattening an email to a string attempts to
2+
replace a non-existent Content-Transfer-Encoding header.

0 commit comments

Comments
 (0)