Skip to content

feat: e-mail messages (.eml, .msg) open read-only, attachments saved from the File menu - #55

Open
mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:feat/mail-messages
Open

mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:feat/mail-messages

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

PRD: e-mail messages in the viewer — phase 1, single messages

Summary

.eml (also Apple Mail's .emlx) and Outlook .msg / .oft files open read-only in DocViewer, the same way Visio drawings and iWork files do. The worker reads the message and builds a DOCX of it, so pages, zoom, find, print, PDF and Download as Word all work without new view code. Attachments are listed in the message and can be saved from the File menu. Nothing is fetched from the network.

This PR is phase 1 of a larger mail track. Archives (.pst, .ost, .olm, .mbox) need a view of their own and are planned as later phases (see Phasing).

Problem

People keep messages as files: Outlook's Save as writes .msg, other mail clients and exports write .eml, and both are common attachments in tickets, legal and compliance work, and document management systems. Today the engine refuses them:

  • a .msg is a compound file without a Word, Excel or PowerPoint stream ("Not a Word, Excel or PowerPoint file")
  • an .eml opens as plain text, showing its raw MIME source, base64 and all

Hosts that embed the viewer have no way to show these files without a server or a second library.

Goals

  1. Open .eml, .emlx, .msg and .oft read-only, with the subject, sender, recipients, date, body and attachments shown as a mail client shows them.
  2. Keep the engine's promises: no server, no new runtime dependency, nothing leaves the browser, and no network requests caused by a message's content.
  3. Reuse the existing pipeline (compound file reader, HTML reader, RTF converter, DOCX view) rather than adding a new view kind.
  4. Let users get the attachments out.
  5. Write the header in the viewer's language.

Non-goals (phase 1)

  • Mail archives and mailboxes: .pst, .ost, .olm, .mbox
  • Editing or saving back to .eml / .msg (saving gives .docx, as for Visio)
  • Calendar, contact and task fields of non-message Outlook items (the item is shown as a message, labeled with its kind)
  • winmail.dat (TNEF) unpacking, opening attachments inside the viewer
  • S/MIME or PGP decryption and signature checks

Users and use cases

Who Need
End user of a host app (DMS, ticketing, CRM) Click a stored .msg / .eml and read it without Outlook
Legal, compliance, support Read a message as sent (headers, date, recipients), print it or save it as PDF, get its attachments
Host developer One src for every file type: no special handling for mail, and the core API for attachments

Requirements

# Requirement Priority Status
R1 .eml / .emlx: headers with encoded words (RFC 2047) and RFC 2231 parameters, 8-bit UTF-8 headers, nested multiparts (alternative, related, mixed, signed, report), base64 / quoted-printable, any charset TextDecoder knows, format=flowed Must ✅
R2 .msg / .oft: MAPI properties (MS-OXMSG): sender (SMTP, Exchange-only senders by name), To / Cc / Bcc from recipient storages (falling back to transport headers, then display strings), sent / received time, importance, message class Must ✅
R3 Outlook bodies: PR_HTML, else the HTML or text Outlook wraps in compressed RTF (MS-OXRTFCP LZFu / MELA, MS-OXRTFEX \fromhtml1 / \fromtext), else the RTF itself through the existing RTF converter, else PR_BODY Must ✅
R4 HTML bodies keep headings, lists, links, text formatting, tables (spans, borders, cell colors) and the pictures the message carries (cid: and data:, PNG / JPEG / GIF / BMP / WebP) Must ✅
R5 Plain-text bodies: a paragraph per line, URLs as links, quoted lines in grey Should ✅
R6 Attached messages (message/rfc822, attached Outlook items) shown after the message, recursively (depth-limited) Should ✅
R7 Attachments listed in the header (name, size). Pictures the body shows and Outlook's hidden attachments are not listed Must ✅
R8 Attachments saved as they are from File → Attachments, and through the core API (mail.attachment) Must ✅
R9 Read-only: an EML / MSG badge, no Edit button even with editable; Download as Word, PDF, web page, text and print work Must ✅
R10 Detection by content: an Outlook item is a compound file with a property stream; an Internet message is recognized by its extension, message/rfc822, or (no extension) its headers. Browser-saved MHTML is not mistaken for mail. A .msg that is MIME text opens as .eml, and one that is neither says so Must ✅
R11 Header words from the viewer's strings (strings.mail, German included in the de table), dates in the viewer's locale Should ✅
R12 File → Open and the picker accept .eml, .emlx, .msg, .oft Must ✅
R13 Encrypted messages say so instead of showing a body Should ✅

Non-functional requirements

  • Privacy: no request is made because of a message's content. Remote pictures (tracking pixels included) are left out, and the document says "Pictures from the web are not shown". The browser test routes every https:// request and asserts none happens.
  • Security: the HTML is read by the existing DOM-free reader. Scripts, styles and forms are not read, links pass safeHref (no javascript:), and the HTML is never put into the page.
  • Robustness: damaged, cut-short or crafted files fail with a message or show what can be read. They don't hang or crash. Part nesting (32), attached-item nesting (16) and the RTF decompression buffer (bounded by what the compressed data can expand to) are limited.
  • Bundle size: the parsers are only in the worker bundle. dist/index.js and dist/core.js don't include them; the React side imports only the extension table.
  • No new dependencies.

UX

The document reads top to bottom:

  1. the subject (Heading 1), then the item kind (Meeting request, Delivery report…) and importance (High importance in red), when they apply
  2. a borderless two-column table: From, Sent, To, Cc, Bcc, Attachments (addresses are mailto: links)
  3. notices (remote pictures not shown, encrypted)
  4. a hairline, then the body
  5. each attached message: Attached message, then the same layout

File menu: an Attachments section lists each attachment with its size; choosing one saves it.

Technical design

src/core/mail/
  types.ts    message model, MailTexts (English defaults), MAIL_EXT (no imports: the UI uses it)
  mime.ts     .eml / .emlx reader
  msg.ts      .msg / .oft reader on legacy/cfb.ts
  rtf.ts      LZFu / MELA decompression, HTML / text de-encapsulation on the RTF lexer
  picture.ts  picture format and size from their bytes
  docx.ts     message → paragraphs → DOCX
  index.ts    detection, openMail()
  • Engine: before the legacy and plain branches, a compound file that is an Outlook item, or text that is an Internet message, goes to openMail(). The DOCX it builds opens like any converted file, with from: 'eml' | 'msg' (which already makes the viewer read-only). The engine keeps the attachments for mail.attachment.
  • parasToDocx (shared): the body loop moved into a BodyWriter that also writes tables (grid from spans, gridSpan / vMerge, borders, cell fill), raster pictures and list levels, plus a rule under a paragraph. Existing output (SVG diagrams as media/diagramN.svg) is unchanged.
  • htmlToParas (shared): rich mode also gives a table's border and a cell's fill. These fields are new and the clipboard path ignores them.
  • RTF converter: rtfToDocx(bytes, wrap?) puts body XML before and after the document's own, for the header of an Outlook rich-text message. Without wrap nothing changes.

API changes (additive)

  • OpenResult.from gains 'eml' | 'msg'. New OpenResult.mail: { subject, attachments: [{ name, type, size }] }.
  • New method 'mail.attachment'(index): Blob.
  • New open parameter mail?: { texts?: Partial<MailTexts>; locale?: string } (the viewer passes its strings).
  • Strings gains from.eml, from.msg, mail.* and file.attachments. Hosts with their own full table get the English defaults for the new keys through the usual merge.

Testing

  • test/mail.test.ts (23 tests): headers, addresses, parameters, flowed text, MIME structures (alternative, related, mixed, signed, forwarded, cut short, encrypted), .emlx, .msg (recipients, Exchange senders, display-string fallback, message classes, attachment order, attached items), LZFu (literals, dictionary references, MELA, damaged input), RTF de-encapsulation (\fromhtml1, \fromtext, native RTF wrapped by the converter), the built document (header table, pictures, links, cell shading, localized header, remote pictures, script links), detection (no name, message/rfc822, MHTML, a .msg that is MIME), picture sizes.
  • e2e/mail.spec.ts (3 tests, Chromium): .eml and .msg render read-only with their pictures and tables, no network request, attachments and Download as Word save, File → Open takes a mail file, the header in German.
  • Unchanged and passing: security, plain, docx-table, viewer, legacy and file-menu e2e specs.
  • Full bun test: everything passes except 7 code-page tests (Windows-1258 CSV, BIFF5, RTF \ansicpg / \mac). These fail the same way on main on this machine, so they are not caused by this PR.
  • Samples: bun scripts/mail-samples.ts writes playground/public/samples/sample.eml and sample.msg from test/mail-builder.ts. Both are in the playground's sample list.

Phasing

Phase Scope Notes
1 (this PR) .eml, .emlx, .msg, .oft Read-only document. Most of it reuses existing parts
2 Archive view + .mbox New mail view kind: folder tree, virtualized message list, reading pane. The archive is scanned in one pass to index where messages start, using the same ranged reads as large CSVs. Engine.open needs a branch that is not an OPC package
3 .pst, .ost, .olm PST: NDB B-trees, LTP heaps and tables, ANSI and Unicode variants, PST's simple encryption. OST from Outlook 2013 on uses 4 KB compressed pages, the hardest part. OLM is a ZIP of XML
4 Extras winmail.dat (TNEF), .ics / .vcf, meeting and contact fields, opening attachments in the viewer, search

Risks and open questions

  • HTML layout fidelity: newsletter-style mail relies on CSS and nested tables. Phase 1 keeps text, top-level tables, colors and pictures. Nested tables become lines of text and <style> rules are not applied. Is that acceptable for a view-only reader, or should nested tables be written as nested DOCX tables before phase 2?
  • \*\mhtmltag: both the cid: tag and the original tag are kept. The non-cid: picture is then left out because it isn't a data: URL. This is harmless in practice, but untested against a wide corpus.
  • Dates: shown in the browser's time zone, as a mail client does. A forensic reader might want the sender's offset too. That could be an option later.
  • Real-world corpus: tests use generated files. Testing against real Outlook / Gmail / Thunderbird exports before release would be good.

Success measures

  • .eml / .msg opened through src or File → Open show a readable message instead of an error or raw MIME.
  • No network request caused by message content (asserted in e2e).
  • No change to the size of the UI bundle; no new dependency.

🤖 Generated with Claude Code

…from the File menu

Internet messages (.eml, Apple Mail's .emlx) and Outlook items (.msg, .oft
templates) open as a read-only document, like Visio drawings and iWork files:
the worker reads the message and builds a DOCX of it.

- src/core/mail: a MIME reader (encoded words, RFC 2231 parameters, nested
  multiparts, charsets, format=flowed, forwarded messages), an Outlook item
  reader on the existing compound file reader (MAPI properties, recipients,
  attachments, attached items), compressed RTF (LZFu / MELA) and the HTML or
  text Outlook wraps in RTF; native RTF bodies go through the RTF converter
  with the header around them
- the document: subject, item kind and importance, a From / Sent / To / Cc /
  Bcc / Attachments table, the body (HTML with tables, cell colors and the
  pictures the message carries; plain text with links), attached messages
- pictures an HTML body loads from the web are never fetched, and the
  document says so
- parasToDocx writes tables, raster pictures, list levels and rules
- File menu: an Attachments section saves each attachment; core API:
  `info.mail` and `mail.attachment`; the header's words follow the viewer's
  strings (`strings.mail`, German included) and locale

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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