Skip to content

Fix IMAP message body decoding for declared charsets - #4

Closed
Code-suphub wants to merge 1 commit into
trymetis:mainfrom
Code-suphub:fix/imap-body-charset
Closed

Code-suphub wants to merge 1 commit into
trymetis:mainfrom
Code-suphub:fix/imap-body-charset

Conversation

@Code-suphub

Copy link
Copy Markdown

Fixes #3

Summary

  • Decode IMAP HTML and plain-text bodies using the downloaded MIME part's charset, with the BODYSTRUCTURE charset as a fallback.
  • Preserve UTF-8 decoding for parts with a missing or unsupported declaration.
  • Add a synthetic multipart regression fixture with a Base64 plain-text part and a quoted-printable HTML part, both encoded in GB2312.

Existing cached body text may need to be fetched again before the corrected rendering is visible.

Verification

  • bun run typecheck
  • bun run lint
  • Read the synthetic GB2312 fixture through the patched IMAP adapter against local Dovecot and verified both body formats display 你好.

Signed-off-by: lixuefei <78542984+Code-suphub@users.noreply.github.com>
@trymetis

Copy link
Copy Markdown
Owner

Thanks for tracking this down and for the clear reproduction. You found the right cause: downloadMany() returns each part in its original charset, and the IMAP adapter reads it as UTF-8. We can't merge this as it is:

  • It breaks mail that displays correctly today. A body labelled iso-8859-1 whose bytes are really UTF-8, which older mail programs often send, now decodes as Windows-1252. In the existing enc-latin1-label-utf8-bytes case, Café becomes Café and 日本語 becomes 日本語.
  • Some charsets stay garbled. TextDecoder only knows the WHATWG labels, so mail labelled cp936, utf-7, cp850 or ibm437 still falls back to UTF-8.
  • Messages opened before the upgrade keep showing the garbled copy that was cached when they were first opened.
  • The new test covers only GB2312, so neither of the first two cases is caught. It also reuses date index 31, which enc-latin1-label-utf8-bytes already has.
  • Smaller points: the changelog line belongs under ### Fixed and conflicts with main, and AGENTS.md doesn't allow comments in code.

The fix has to cover every charset, so we're making the broader change ourselves for 0.4.2. Closing this, and #3 stays open until it ships.

@trymetis trymetis closed this Nov 24, 2026
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.

IMAP message bodies in non-UTF-8 charsets render as garbled text

2 participants