Skip to content

fix(mailcompose): encode every Subject chunk so folding adds no space - #283

Merged
itsjeremyjohnson merged 1 commit into
mainfrom
fix/mailcompose-subject-encoded-words
Nov 9, 2026
Merged

itsjeremyjohnson merged 1 commit into
mainfrom
fix/mailcompose-subject-encoded-words

Conversation

@itsjeremyjohnson

Copy link
Copy Markdown
Collaborator

Refs itsjeremyjohnson/fleet#805

Problem

A mail_compose_send Subject ... renewal charged Nov 6 arrived as ... renewal charge d Nov 6.

encodedHeaderWords split the Subject with mime.QEncoding.Encode. That function returns pure-ASCII input unchanged, so an ASCII chunk stayed plain text next to an encoded-word. RFC 2047 section 6.2 keeps whitespace between an encoded-word and plain text, so the joining space or fold showed up in the decoded Subject. A leading ASCII chunk gave a doubled space instead.

Change

  • internal/mailcompose/mime.go: new qEncodedWord always emits =?utf-8?q?...?=. Its output matches mime.QEncoding byte for byte for non-ASCII input. Every Subject chunk now goes through it, split on UTF-8 rune boundaries.
  • Subject lines fold at 76 bytes (RFC 2047 section 2 limit for lines with encoded-words), so encoded-words are at most 67 bytes. writeStructuredHeader takes the line limit as a parameter. To/Cc/Bcc and References keep 998.
  • ASCII-only subjects still use the plain writeHeader path. Output is unchanged.

From/To display names go through net/mail Address.String(), which emits only adjacent encoded-words. Attachment filenames use RFC 2231 filename*= via mime.FormatMediaType. Both round-trip exactly, and the new tests cover them. Nothing else in the repo builds =?utf-8? headers.

Testing

  • New TestSubjectRoundTripsExactly renders through renderMIME, parses with net/mail and mime.WordDecoder, and requires an exact decoded match. Cases: the incident subject, a trailing-ASCII case, a leading-ASCII case, a 177-byte mixed subject that folds onto 4 lines, and an ASCII-only subject whose raw line is pinned. Every Subject line must be at most 78 bytes and every encoded-word at most 75.
  • New TestDisplayNamesAndFilenameRoundTripExactly covers long non-ASCII From/To names and a non-ASCII attachment filename.
  • Before the fix, 4 of the 5 subject cases fail (charge d, account – Nov, abcd efghij, for the Q4). With the fix, all pass.
  • make ci (fmt-check, golangci-lint v2.13.2, go test ./...) passes on go1.26.0.

🤖 Generated with Claude Code

encodedHeaderWords used mime.QEncoding.Encode, which returns pure-ASCII
input unchanged. A Subject chunk without non-ASCII bytes became plain
text next to an encoded-word, and RFC 2047 section 6.2 keeps the
whitespace between them, so "renewal charged Nov 6" decoded as
"renewal charge d Nov 6".

Wrap every chunk in a Q encoded-word with output identical to
mime.QEncoding for non-ASCII input, and fold Subject lines at the
RFC 2047 section 2 limit of 76 bytes. ASCII-only subjects still use the
plain header path and are unchanged.

Refs itsjeremyjohnson/fleet#805

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant