Skip to content

fix: set HTML bodies via Mail's html content, and keep the signature when editing a draft - #99

Closed
herbeckrobin wants to merge 10 commits into
patrickfreyer:mainfrom
herbeckrobin:fix/mail-tool-audit
Closed

herbeckrobin wants to merge 10 commits into
patrickfreyer:mainfrom
herbeckrobin:fix/mail-tool-audit

Conversation

@herbeckrobin

Copy link
Copy Markdown
Contributor

Stacked on #98 — only the last two commits belong to this PR. GitHub cannot take a fork branch as a base, so the seven commits below 7e5ba65 are #98's and will disappear from this diff once that one is merged.

Both problems were measured against Mail 16.0 (3864.600.51.1.1) on macOS before anything was changed, using drafts that are still on disk.

set html content instead of the clipboard

compose_email(body_html=...) converted the markup to an NSAttributedString, wrote it back to the pasteboard as RTF, opened a visible compose window and drove Cmd+A / Cmd+V through System Events.

  • it took keyboard focus away from whatever the user was doing
  • it aborted whenever Mail did not come to the front
  • the RTF round-trip discards CSS, so every message arrived as 12px black Helvetica regardless of the HTML that went in

Mail's html content property takes the markup verbatim. A draft written through the new path came back byte-identical: <h2 style="color:#3399ff">, <b>, <a href>, <ul><li>, umlauts. The property also leaves message signature alone, so Mail attaches the real signature and the compose window still names it. Nothing is typed any more, and mode="send" uses the send command instead of a Cmd+Shift+D keystroke.

The HTML reaches AppleScript through a temp file read as «class utf8»: no escaping, no length ceiling, and multibyte text survives.

reply_to_email and forward_email deliberately keep the paste route. They have to preserve the quoted original Mail puts in the body, and set html content would replace it. Their focus guards and tests are untouched.

Editing a draft no longer costs it its signature

manage_drafts(action="update") rebuilt the message from content of theDraft — Mail's plain-text rendering, in which the signature is already reduced to unformatted text — and then set message signature to missing value so that flattened copy would not be doubled.

The result had no X-Apple-Mail-Signature header, Mail's compose window reported "Signature: None", and the signature had lost its table, image and colours. Reported as "the signature is gone and everything is shifted".

The rebuild now takes the real HTML out of source of theDraft, cuts the baked-in <div id="AppleMailSignature"> block out of it, and leaves message signature alone so Mail attaches the live one. When the HTML cannot be recovered the old plain-text rebuild runs and the return value says so instead of pretending nothing was lost.

Mail's share wrapper

macOS 15+ Mail wraps every AppleScript-supplied body in <div class="Apple-Mail-URLShareWrapperClass"><blockquote type="cite">, which makes a fresh message look quoted in clients that ignore the wrapper's inline inherit styles.

Nothing in this package writes that markup and no ordering of the calls avoids it — a content property, a later set content, set html content and a GUI paste into a scripted compose window were all measured and produce it identically. It is documented in the README under "Known Mail.app limitations" rather than papered over.

What was fixable is the stacking: feeding a stored body back through the composer added a layer on every edit (a draft edited twice measured two blockquotes deep). The reuse path peels the existing layers off first, with a nesting-aware scanner that leaves unbalanced markup alone rather than cutting at a guessed offset.

Verification

python -m pytest tests/ -q (191) and python scripts/check_versions.py pass. 21 new tests cover the wrapper and signature helpers, the three compose paths, and the fallback when the stored HTML cannot be read. The three older tests that guarded compose's paste route are replaced by tests asserting that nothing is typed any more.

https://claude.ai/code/session_01BxRqpRbe7bC31Sj6yuJ8ic

Two problems, one cause: the flag was applied by deleting tools rather
than by gating the sending path.

1. The guard in manage_drafts(action="send") never fired. compose.py did
   `from apple_mail_mcp.server import READ_ONLY`, binding the value False
   at import, while __main__ assigns server.READ_ONLY afterwards. The
   module now reads the flag through the server module at call time.

2. --read-only removed compose_email and reply_to_email outright, even
   though their "draft" and "open" modes never deliver anything. Those
   two are also the only tools in the compose surface that accept
   attachments, so read-only mode left no supported way to attach a file
   to a draft. They now stay registered and refuse mode="send"
   themselves. forward_email has no non-sending mode and is still
   removed.

Adds regression coverage for both, including one test that fails if the
flag is ever bound at import time again.
@patrickfreyer

Copy link
Copy Markdown
Owner

This review was written by Claude (AI assistant) on Patrick's behalf.

Thanks @herbeckrobin. This builds on #98 (pr-98 is an ancestor) and adds three commits, not two as the description says: 7e5ba65 (draft signature), a869cae (html content) and b22f57a (native reply window). 196 tests pass. Since b22f57a reverses #98's reply design, we'd suggest landing the small fixes from #98 first and then one reply/compose PR against epic #80.

Findings (paths on this branch):

  1. Hardcoded German quote header. compose.py:778 REPLY_QUOTE_HEADER = "Am {date} schrieb {sender}:" is still used when Mail can't get focus. It should come from Mail's own reply or be localized.
  2. Unguarded set html content. Lines 650-660 don't check that the body landed and have no fallback, and body_plain is ignored (line 577). If this undocumented property silently no-ops, mode="send" sends an empty email. Please read back, or set content first as a fallback. Same pattern in the draft rebuild at line 1896.
  3. Can close the user's own window. Line 1231 takes id of window 1 after a fixed delay 1. If Mail is slow that's the user's window, and the error path at line 1294 closes it with saving no. Waiting until a new window id appears would avoid this.
  4. manage_drafts(action="update") breaks threading. Lines 1918-1936 rebuild every draft as a fresh outgoing message, so reply drafts lose In-Reply-To/References with no warning. That conflicts with Epic: reply-safety redesign (verified drafts, body placement, simpler compose surface) #80 pillars 1 and 4. Refusing (or warning) on reply drafts would be safer.
  5. Read-only docs out of sync. __main__.py no longer removes any tool, so forward_email (which gained mode) stays registered, while README:149 and CHANGELOG:34 say it's "removed entirely". CHANGELOG:20 also says reply/forward "keep the paste route", which the next entry contradicts.

Also: the new tests assert script strings only, so nothing covers the window-id race or a silent html content no-op. It conflicts with #101 in compose.py; both target #100, so only one approach should land.

@fredrikmindnes

Copy link
Copy Markdown

Reproduced the close window 1 problem in draft-mode reply_to_email today (macOS, Mail, an Exchange account), so +1 for getting this merged.

When Mail isn't the frontmost app, reply_to_email(mode="draft") opens the reply window, the body paste doesn't land, and close window 1 saving yes doesn't hit the reply. The window stays open with an empty body. Closing it triggers Mail's "Save this message as a draft?" prompt, and nothing new appears in Drafts. The tool still returns "Reply saved as draft!"

This happens on every draft reply. save replyMessage as in this PR is the right fix.

Copy link
Copy Markdown
Owner

Posted by Claude (AI maintainer agent) on Patrick's behalf.

Closing this for now, @herbeckrobin, since there's been no reply to the 2026-10-14 review for 42 days and the branch now conflicts with main. Thank you for the work. It shaped several things that have landed or are landing since:

If you want to pick any of these up again, a small PR against current main for one of them is very welcome. The unguarded html content and the window-id race from the review are the two things it would need to address. Reopening is fine too.


Generated by Claude Code

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.

3 participants