You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
email ContentManager mutates messages after failed content validation and accepts unknown byte CTE values #157254
EmailMessage.set_content() clears existing content before built-in content validation completes. A failing text-content request therefore destroys the original payload. Separately, the bytes content handler accepts arbitrary cte strings and serializes them as Content-Transfer-Encoding, even though the documented API defines a fixed set of permitted values. Together, these behaviors let invalid content requests corrupt an existing message or produce invalid outgoing MIME metadata.
Reproduction Code
fromemail.messageimportEmailMessage# Failed text setup loses the prior message content.msg=EmailMessage()
msg.set_content("original")
before=msg.as_bytes()
try:
msg.set_content("replacement", charset="does-not-exist")
exceptLookupErrorasexc:
print(type(exc).__name__, exc)
print(msg.as_bytes() ==before)
print(msg.as_string())
# Unsupported byte CTE is accepted and emitted unchanged.msg=EmailMessage()
msg.set_content(
b"abc",
maintype="application",
subtype="octet-stream",
cte="not-a-transfer-encoding",
)
print(msg.as_bytes().decode("ascii"))
Actual Behavior
The first request raises LookupError: unknown encoding: does-not-exist, then prints False. The serialized message no longer contains original; it retains MIME headers added before the failure.
A failing add_attachment("replacement", charset="does-not-exist") also leaves the parent converted to a one-part multipart/mixed message.
Expected Behavior
Invalid content requests should raise before changing the target message. In particular, a failed set_content() or add_attachment() call should preserve the prior serialized message and MIME structure.
The bytes content handler should reject unsupported CTE values with ValueError, consistently with the documented CTE set and the text content handler, rather than emitting an unrecognized Content-Transfer-Encoding header.
Activity
lpyu001 commentedon Sep 10, 2026
pythongh-157254: Preserve email content on failure and reject unsuppo…