Skip to content

Commit bf83822

Browse files
authored
bpo-27321 Fix email.generator.py to not replace a non-existent header. (GH-18074)
This PR replaces #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 #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
1 parent 1438c2a commit bf83822

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
@@ -312,6 +312,41 @@ def test_as_string_policy(self):
312312
g.flatten(msg)
313313
self.assertEqual(fullrepr, s.getvalue())
314314

315+
def test_nonascii_as_string_without_cte(self):
316+
m = textwrap.dedent("""\
317+
MIME-Version: 1.0
318+
Content-type: text/plain; charset="iso-8859-1"
319+
320+
Test if non-ascii messages with no Content-Transfer-Encoding set
321+
can be as_string'd:
322+
Föö bär
323+
""")
324+
source = m.encode('iso-8859-1')
325+
expected = textwrap.dedent("""\
326+
MIME-Version: 1.0
327+
Content-type: text/plain; charset="iso-8859-1"
328+
Content-Transfer-Encoding: quoted-printable
329+
330+
Test if non-ascii messages with no Content-Transfer-Encoding set
331+
can be as_string'd:
332+
F=F6=F6 b=E4r
333+
""")
334+
msg = email.message_from_bytes(source)
335+
self.assertEqual(msg.as_string(), expected)
336+
337+
def test_nonascii_as_string_without_content_type_and_cte(self):
338+
m = textwrap.dedent("""\
339+
MIME-Version: 1.0
340+
341+
Test if non-ascii messages with no Content-Type nor
342+
Content-Transfer-Encoding set can be as_string'd:
343+
Föö bär
344+
""")
345+
source = m.encode('iso-8859-1')
346+
expected = source.decode('ascii', 'replace')
347+
msg = email.message_from_bytes(source)
348+
self.assertEqual(msg.as_string(), expected)
349+
315350
def test_as_bytes(self):
316351
msg = self._msgobj('msg_01.txt')
317352
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)