cpython icon indicating copy to clipboard operation
cpython copied to clipboard

email.message generates invalid encoded header

Open chrullrich opened this issue 5 months ago • 1 comments

Bug report

Bug description:

A message header generated by email.message.Message can contain invalid RFC 2047 encoded-words.

import sys
from email.message import Message
from email.policy import SMTP

msg = Message(policy=SMTP)
msg["Subject"] = "Re: some few filler words here RE: Routeraustausch und übriggebliebene Glasfaser"
sys.stdout.buffer.write(msg.as_bytes())

Result:

Subject: Re: some few filler words here RE: Routeraustausch und =?utf-8?q??=
 =?utf-8?q?=C3=BCbriggebliebene?= Glasfaser

The encoded-word in the first line is invalid. Per RFC 2047 the "encoded-text" must not be empty:

encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
encoded-text = 1*<Any printable ASCII character other than "?"
                 or SPACE>

The error does not appear in this case with the default policy because the header value is split up entirely differently.

CPython versions tested on:

3.13, 3.12

Operating systems tested on:

Windows, Other

chrullrich avatar Jun 27 '25 19:06 chrullrich

LGTM, would you like to open a PR to solve it? Or I think I can try to open one.

LamentXU123 avatar Jun 28 '25 03:06 LamentXU123

@LamentXU123 "LGTM" means "Let's get this merged" which is usually only used when a PR has been submitted, reviewed, and accepted by a core developer (or "Looks good to me", but in general LGTM is for a PR approval).

picnixz avatar Jun 29 '25 13:06 picnixz

@LamentXU123 "LGTM" means "Let's get this merged" which is usually only used when a PR has been submitted, reviewed, and accepted by a core developer (or "Looks good to me", but in general LGTM is for a PR approval).

Thanks to remind. I've used LGTM as looks good to me for decades, but I will not use this anymore since it will cause some confusion >_<

LamentXU123 avatar Jun 29 '25 13:06 LamentXU123