fix(msg): RFC 2047 encode non-ASCII names in address headers#317
Open
lukas-r wants to merge 1 commit into
Open
fix(msg): RFC 2047 encode non-ASCII names in address headers#317lukas-r wants to merge 1 commit into
lukas-r wants to merge 1 commit into
Conversation
str(NameEmail) emits the display name as raw UTF-8, so To/Cc/Bcc/ Reply-To headers built via ", ".join(str(r) for r in ...) carry raw non-ASCII bytes. On older Python versions the stdlib email package silently re-encoded these (issue sabuhish#225); on Python 3.14+ it no longer does, and some SMTP servers (e.g. Office 365) strip the non-ASCII bytes from the recipient display name and reject the message with `550 5.2.254 InvalidRecipientsException`, eventually throttling the sender. Use email.utils.formataddr with charset="utf-8" so display names are RFC 2047 quoted-printable encoded. ASCII names round-trip unchanged. Also pass charset="utf-8" to the existing formataddr call in FastMail._sender so MAIL_FROM_NAME has the same protection. Fixes sabuhish#316 Refs sabuhish#225
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Encode non-ASCII display names in recipient headers (
To,Cc,Bcc,Reply-To,From) viaemail.utils.formataddr(..., charset="utf-8")so they are RFC 2047 quoted-printable on the wire instead of raw UTF-8. ASCII names are unaffected —formataddronly escapes when needed, so existing tests pass unchanged.Reason
Fixes #316 (and addresses the root cause of #225).
str(NameEmail("Lukas Böhm", "lboehm@example.com"))returns'Lukas Böhm <lboehm@example.com>'— raw UTF-8. Older Python versions silently re-encoded such headers; Python 3.14 no longer does. Office 365's SMTP submission server then strips the non-ASCII bytes from the recipient and rejects the message with:Repeated failures push the sender mailbox into a throttled state.
formataddr(("Lukas Böhm", "lboehm@example.com"), charset="utf-8")returns'=?utf-8?q?Lukas_B=C3=B6hm?= <lboehm@example.com>'— RFC 2047 compliant, ASCII-clean.The same
charset="utf-8"is also added to the existingformataddrcall inFastMail._sendersoMAIL_FROM_NAMEgets the same protection.Test
New
test_non_ascii_names_are_rfc2047_encodedintests/test_message.pyasserts that headers for umlaut/diacritic names are ASCII-encodable, contain=?utf-8?MIME markers, and round-trip back to the original Unicode viadecode_header+getaddresses.All 24 tests pass on Python 3.14.