email as_bytes() corruption of headers with SMTPUTF8 policy

Open
#99,927 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

Reproduce the issue with email.message_from_bytes and msg.as_bytes(policy=email.policy.SMTPUTF8) using the example in the report. Trace the email package's header parsing and serialization entry points, then add a regression test that preserves the Unicode display name and matches the expected output.

Written by the indexing model from the issue text.

Description

stdlib topic-email type-bug

Bug report

Reading and then writing RFC2822 messages with SMTPUTF8 policy results in header corruption.

import email
import email.policy

odata = '''From: Unicôde Nâme <example@example.com>
To: Name One <name-one@example.com>, Name Two <name-two@example.com>, Unicôde Nàme <unicode-name@example.com>
Subject: Unicôde Subjéct

This message contains Uniçôde Çôntent.
'''

msg = email.message_from_bytes(odata.encode(), policy=email.policy.SMTPUTF8)

ndata = msg.as_bytes(policy=email.policy.SMTPUTF8)
print(ndata.decode())

Expected output:

From: Unicôde Nâme <example@example.com>
To: Name One <name-one@example.com>, Name Two <name-two@example.com>,
 Unicôde Nàme <unicode-name@example.com>
Subject: Unicôde Subjéct

This message contains Uniçôde Çôntent.

Actual output:

From: Unicôde Nâme <example@example.com>
To: Name One <name-one@example.com>, Name Two <name-two@example.com>,
 =?unknown-8bit?q?Unic=C3=B4de_N=C3=A0me?= <unicode-name@example.com>
Subject: Unicôde Subjéct

This message contains Uniçôde Çôntent.

Your environment

  • CPython versions tested on: Python 3.11.0
  • Operating system and architecture: Linux x86-64, Fedora 37
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.