Repository navigation
fix: set HTML bodies via Mail's html content, and keep the signature when editing a draft - #99
herbeckrobin wants to merge 10 commits into
Conversation
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.
…wurf bearbeiten Claude-Session: https://claude.ai/code/session_01DKjXwP6sh7k12HAZWMuyoT
… Text nachzubauen
|
This review was written by Claude (AI assistant) on Patrick's behalf. Thanks @herbeckrobin. This builds on #98 ( Findings (paths on this branch):
Also: the new tests assert script strings only, so nothing covers the window-id race or a silent |
|
Reproduced the When Mail isn't the frontmost app, This happens on every draft reply. |
|
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 Generated by Claude Code |
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
7e5ba65are #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 contentinstead of the clipboardcompose_email(body_html=...)converted the markup to anNSAttributedString, wrote it back to the pasteboard as RTF, opened a visible compose window and drove Cmd+A / Cmd+V through System Events.Mail's
html contentproperty 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 leavesmessage signaturealone, so Mail attaches the real signature and the compose window still names it. Nothing is typed any more, andmode="send"uses thesendcommand 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_emailandforward_emaildeliberately keep the paste route. They have to preserve the quoted original Mail puts in the body, andset html contentwould 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 fromcontent of theDraft— Mail's plain-text rendering, in which the signature is already reduced to unformatted text — and then setmessage signaturetomissing valueso that flattened copy would not be doubled.The result had no
X-Apple-Mail-Signatureheader, 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 leavesmessage signaturealone 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 inlineinheritstyles.Nothing in this package writes that markup and no ordering of the calls avoids it — a
contentproperty, a laterset content,set html contentand 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) andpython scripts/check_versions.pypass. 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