This skill reads and searches Gmail via CLI using 1Password OAuth to help you manage emails, drafts, and exports efficiently.
npx playbooks add skill terrylica/cc-skills --skill gmail-access
Read and search Gmail programmatically via Claude Code.
> Self-Evolving Skill: This skill improves through use. If instructions are wrong, parameters drifted, or a workaround was needed — fix this file immediately, don't defer. Only update for real, reproducible issues.
CRITICAL: You MUST complete this preflight checklist before running any Gmail commands. Do NOT skip steps.
ls -la "$HOME/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-cli/gmail" 2>/dev/null || echo "BINARY_NOT_FOUND"
If BINARY_NOT_FOUND: Build it first:
cd ~/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-cli && bun run build
echo "GMAIL_OP_UUID: ${GMAIL_OP_UUID:-NOT_SET}"
If NOT_SET: You MUST run the Setup Flow below. Do NOT proceed to Gmail commands.
ALWAYS verify you're accessing the correct email account for the current project.
# Show current project context
echo "=== Gmail Account Context ==="
echo "Working directory: $(pwd)"
echo "GMAIL_OP_UUID: ${GMAIL_OP_UUID}"
# Check where GMAIL_OP_UUID is defined (plain env: shell startup files, this shell, or inline)
echo ""
echo "=== GMAIL_OP_UUID Source ==="
grep -l "GMAIL_OP_UUID" ~/.zshenv ~/.zshrc 2>/dev/null || echo "Not in shell startup files (set in this shell or passed inline)"
# Quick connectivity test — shows the account email from a real email
echo ""
echo "=== Account Verification ==="
$GMAIL_CLI list -n 1 2>&1 | head -5
STOP and confirm with user before proceeding:
list -n 1 output shows the account's inbox — verify this matches the project's intended emailGMAIL_OP_UUID=<uuid> $GMAIL_CLI ...); nothing selects the account from the working directoryMulti-account disambiguation (when GMAIL_OP_UUID is NOT_SET but tokens exist). There is no whoami subcommand; map each cached token UUID to its mailbox by probing, then pick the one that fits the project:
🔴 Do NOT identify a mailbox from list -n 1 --json | jq '.[0].to'. That reads the recipient of the newest message, not the mailbox owner. When the newest item is an outgoing draft or a sent message, it reports the person you wrote TO. Measured 2026-08-18: the <token-uuid-prefix> token probed as an external correspondent's address while the mailbox is in fact you@example.com — a confident wrong answer in the one step whose entire job is to stop you acting on the wrong account.
Ask Gmail who it is instead. users/me/profile is authoritative:
# Which accounts are cached, which mailbox does each ACTUALLY own, and which alias is its DEFAULT?
bun "$HOME/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-accounts.ts"
# one account, machine-readable:
bun "$HOME/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-accounts.ts" --account <uuid> --json
It asks users/me/profile and settings/sendAs with each cached token and never prints a token. Do not read the token files yourself (jq .access_token, cat, python3): gmail-send-guard denies it, because they are bearer credentials for whole mailboxes. You need the send-as list before any --from: verificationStatus must be accepted, and if the alias marked DEFAULT is not the one you want, --from is mandatory, not optional. A message that reaches Gmail with no From line goes out under the DEFAULT alias.
A probe that returns invalid_grant means that account's refresh token is dead (see "Diagnosing invalid_grant"). Pick the working UUID whose mailbox matches the project and pass it inline as GMAIL_OP_UUID=<uuid> on each command. A child project often needs a DIFFERENT account than its parent — verify, never assume the parent's UUID.
# Check cached token exists and is not expired
TOKEN_FILE="$HOME/.claude/tools/gmail-tokens/${GMAIL_OP_UUID}.json"
APP_CREDS="$HOME/.claude/tools/gmail-tokens/${GMAIL_OP_UUID}.app-credentials.json"
echo "Token file: $([ -f "$TOKEN_FILE" ] && echo "EXISTS" || echo "MISSING")"
echo "App credentials: $([ -f "$APP_CREDS" ] && echo "CACHED" || echo "MISSING — will need 1Password on first run")"
If token file is MISSING: First run will open a browser for OAuth consent. This is expected. If app credentials are MISSING: 1Password will be called once to cache client_id/client_secret, then never again.
---
Follow these steps IN ORDER. Use AskUserQuestion at decision points.
command -v op && echo "OP_CLI_INSTALLED" || echo "OP_CLI_MISSING"
If OP_CLI_MISSING: Stop and inform user:
> 1Password CLI is required. Install with: brew install 1password-cli
# Try common vaults — "Claude Automation" for service accounts, "Employee" for interactive
for VAULT in "Claude Automation" "Employee" "Personal"; do
ITEMS=$(op item list --vault "$VAULT" --format json 2>/dev/null | jq -r '.[] | select(.title | test("gmail|oauth|google"; "i")) | "\(.id)\t\(.title)"')
[ -n "$ITEMS" ] && echo "=== Vault: $VAULT ===" && echo "$ITEMS"
done
Parse the output and proceed based on results:
If items found, use AskUserQuestion with discovered items:
AskUserQuestion({
questions: [{
question: "Which 1Password item contains your Gmail OAuth credentials?",
header: "Gmail OAuth",
options: [
// POPULATE FROM op item list RESULTS - example:
{ label: "Gmail API - project-f (56peh...)", description: "OAuth client in Employee vault" },
{ label: "Gmail API - personal (abc12...)", description: "Personal OAuth client" },
],
multiSelect: false
}]
})
If NO items found, use AskUserQuestion to guide setup:
AskUserQuestion({
questions: [{
question: "No Gmail OAuth credentials found in 1Password. How would you like to proceed?",
header: "Setup",
options: [
{ label: "Create new OAuth credentials (Recommended)", description: "I'll guide you through Google Cloud Console setup" },
{ label: "I have credentials elsewhere", description: "Help me add them to 1Password" },
{ label: "Skip for now", description: "I'll set this up later" }
],
multiSelect: false
}]
})
After user selects an item (with UUID), use AskUserQuestion:
AskUserQuestion({
questions: [{
question: "Where should GMAIL_OP_UUID be set?",
header: "Configure",
options: [
{ label: "This session only (Recommended)", description: "export GMAIL_OP_UUID in the current shell; nothing written to disk" },
{ label: "Per command", description: "Pass GMAIL_OP_UUID=<uuid> inline on every gmail call; safest when projects use different mailboxes" }
],
multiSelect: false
}]
})
If "This session only": run export GMAIL_OP_UUID=<selected-uuid>.
If "Per command": prefix each call, e.g. GMAIL_OP_UUID=<selected-uuid> $GMAIL_CLI list -n 1.
Never write it into an env file for a background bot or digest on this machine: those run in a private deployment with its own secret store, and the laptop launchd jobs they replaced were retired on 2026-09-24.
echo "GMAIL_OP_UUID: ${GMAIL_OP_UUID:-NOT_SET}"
GMAIL_OP_UUID="${GMAIL_OP_UUID}" $HOME/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-cli/gmail list -n 1
If OAuth prompt appears: This is expected on first run. Browser will open for Google consent.
---
GMAIL_CLI="$HOME/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-cli/gmail"
# List recent emails
$GMAIL_CLI list -n 10
# Search emails
$GMAIL_CLI search "from:someone@example.com" -n 20
# Search with date range
$GMAIL_CLI search "from:alice after:2026/01/27" -n 10
# Read specific email with full body
$GMAIL_CLI read <message_id>
# Read and download inline images (copy-pasted screenshots in compose)
$GMAIL_CLI read <message_id> --save-images
# Download inline images to a specific directory
$GMAIL_CLI read <message_id> --save-images --image-dir ./attachments/my-folder/
# Shorthand: --image-dir implies --save-images
$GMAIL_CLI read <message_id> --image-dir ./attachments/my-folder/
# JSON output with image metadata and saved paths
$GMAIL_CLI read <message_id> --save-images --json
# Download REAL file attachments (PDF, docx, csv, …) — distinct from inline images
$GMAIL_CLI read <message_id> --save-attachments
# Download attachments to a specific directory (implies --save-attachments)
$GMAIL_CLI read <message_id> --attachment-dir ./files/case-17402939/
# Export search results to JSON (full body + inlineImages + attachments metadata per message)
$GMAIL_CLI export -q "label:inbox" -o emails.json -n 100
# JSON output (for parsing)
$GMAIL_CLI list -n 10 --json
# Create a draft email
$GMAIL_CLI draft --to "user@example.com" --subject "Hello" --body "Message body"
# Create a draft reply (threads into existing conversation)
$GMAIL_CLI draft --to "user@example.com" --subject "Re: Hello" --body "Reply text" --reply-to <message_id>
# Draft with body loaded from a file (multi-paragraph bodies are awkward via --body)
$GMAIL_CLI draft --to "user@example.com" --subject "Report" --body-file ./email-body.txt
# Draft with file attachments (--attach is repeatable; MIME type guessed from extension)
$GMAIL_CLI draft --to "user@example.com" --subject "Q1 Report" \
--body-file ./email-body.txt \
--attach ./report.pdf \
--attach ./screenshot.png
# Draft reply with both body-file and multiple attachments
$GMAIL_CLI draft --to "user@example.com" --subject "Re: Project" \
--reply-to <message_id> \
--body-file ./reply.txt \
--attach ./diagram.pdf
# List drafts with their draft IDs
$GMAIL_CLI drafts -n 10
# Replace a draft in place (delete + recreate — the draft ID CHANGES)
$GMAIL_CLI draft-update <draft_id> --to "user@example.com" --from "me@example.com" \
--subject "Same subject" --body-file ./revised-body.txt
drafts --json field namesThe identifier field is draftId, not id. A .id selector silently yields null for every row rather than erroring, so a jq pipeline built on it looks like it worked and hands you nothing:
# WRONG — .id does not exist; prints "null" per draft and fails silently
$GMAIL_CLI drafts -n 10 --json | jq -r '.[] | "\(.id)\t\(.subject)"'
# RIGHT
$GMAIL_CLI drafts -n 10 --json | jq -r '.[] | "\(.draftId)\t\(.subject)"'
Full row shape: date, draftId, from, messageId, snippet, subject, threadId, to. Note the two distinct identifiers — draftId is what draft-update and draft-delete take; messageId is what read takes. Passing one where the other belongs fails or returns the wrong record.
draft-update returns a NEW draftId (it deletes and recreates). Any ID you noted earlier is dead the moment you update — re-list before a second update, and never cache a draft ID across edits.
Convention for every reply and every draft that quotes someone (the person being answered, an earlier message, a document): quote their words verbatim, each passage in its own > block, and put your answer underneath, outside the block. Never paraphrase inside a quotation, never correct their spelling, and never merge two of their passages into one. When a reply answers several points, alternate: one quoted passage, then the answer to it.
> the exact words being answered, copied as written
The answer to that passage.
The canonical builder (scripts/gmail-draft.ts) renders each > block as a <blockquote> in its own colour with a left bar (muted purple, italic; QUOTE_BLOCK_STYLE) in the HTML part, and keeps the > prefix in the plain-text part, so the reader sees at a glance whose words are whose. A block is a quotation only when every line in it starts with > (up to three spaces of indent); a stray > in prose stays prose. Do not hand-style quotes with HTML: the builder does it.
bun "$HOME/.claude/plugins/marketplaces/cc-skills/plugins/gmail-commander/scripts/gmail-draft.ts" \
--account <token-uuid> \
--body ./reply.md \
--from 'Jane Doe <alias@example.com>' \
--reply-to <messageId> \
--to 'Alex Roe <alex@example.com>' \
--attach ./signed-agreement.pdf \
--replace <superseded draftId>
--attach is repeatable and optional; --to, --cc and --subject are inherited from --reply-to when omitted; --replace deletes the draft this one supersedes, only after the new one has passed verification. Before it prints its one JSON line the builder proves, against what Gmail actually stored: the Subject round-trips, the HTML part exists (byte-identical when --html is used) with no forced mid-sentence line breaks, every attachment's SHA-256 equals the file sent, and the thread holds exactly one draft (exit 1 naming the others when it does not; delete the superseded ones before sending). The output lists each attachment with its byte count and SHA-256.
Two things the converter will not do, and what to do instead:
<br>, so Best regards, / name / title on three lines arrive as one line. Either give each line its own paragraph, or pass --html with an authored HTML part: render the body with the builder's own loadDraftParts, change only the sign-off to use <br>, and pass that file. The builder refuses an authored part that drops or reorders any word of the --body text, so the two parts cannot diverge.On … wrote: line; see the attribution doctrine below.| Path | > quotes in the HTML part | File attachments | --cc | Read-back verification |
| ---------------------------------------- | ------------------------------------------------------------- | ----------------------------------- | ------ | -------------------------------------------------------------------------------- |
| scripts/gmail-draft.ts | Styled <blockquote> | --attach, repeatable (2026-10-30) | Yes | Built in (subject, HTML, forced breaks, attachment hashes, one draft per thread) |
| gmail draft / draft-update (the CLI) | Unstyled: > is escaped and each line kept as plain text | --attach, repeatable | No | None; read it back yourself |
The CLI is fine for a quick plain note to yourself. Every reply that quotes someone goes through gmail-draft.ts. Do not attach a file through Gmail's web compose afterwards: on save Gmail regenerates the plain part from the HTML and the > marks are lost (measured 2026-10-30, before --attach existed).
After creating a draft, confirm it independently with read <messageId> --json --attachment-dir <dir>: every word equal to your source file, zero markdown markers, every > line present, and each downloaded attachment's hash equal to the original.
Reading an email and knowing who wrote which line are two different problems. The second one is harder, its answer is not in the text you get back by default, and when it goes wrong it does not throw — it produces a fluent, confident summary in which your own sentences are attributed to your correspondent, or half of what they wrote is silently missing. Every failure recorded in this section was of that shape: a wrong query returning an empty result, read as an empty world.
This applies by default. If a message is a reply — it has quoted history — and you intend to quote, summarise, attribute, decide, or act on what the sender said, you run the protocol below. It does not apply to listing, searching, triage, or counting.
Three signals, nested, in descending order of trust. Using the wrong one as your primary is how both halves of the 2026-08-25 incident happened.
| Signal | Coverage | Where it lives | Trust |
| ----------------------------------------- | -------------------------------------------------------- | ------------------------------------ | ------------------------------------------------- |
| Quote depth | Complete — captures top matter AND inline insertions | text/plain: lines NOT prefixed > | Primary. But see the caveats — some clients lie |
| Colour | Proper subset — insertions inside a quoted region only | text/html ONLY | Disambiguates exactly where depth gets unreliable |
| Typed marker (>>>PT, @@@, [JS]) | Proper subset of colour — flags where a block STARTS | either part | Weakest. Never sufficient alone |
bun scripts/attribution-parse.ts --id <messageId> --token-cmd '<cmd printing an access token>'
It prints which signals are actually present, what it attributes to the sender and why, and — the important part — what it cannot attribute. It reports CONFLICT when signals disagree and UNKNOWN when a line at quoted depth carries neither colour nor marker, because in that case the line genuinely could belong to either party. UNKNOWN is a correct answer. A tool that always decides is a tool that is sometimes confidently wrong.
🔴 It can also be confidently wrong when the quoted history carries no > at all. Measured 2026-10-30: a top-posted reply whose client put the earlier message under an On <date>, <name> wrote: line without > prefixes came back with every one of its 100 lines "Attributed to the sender" at quote depth 0, including the whole of OUR earlier letter quoted below that line, with no UNKNOWN and no CONFLICT. Before quoting or attributing, find the On … wrote: (or -----Original Message-----, From: … Sent: …) boundary yourself and treat everything below it as not the sender's, whatever the parser says. Open defect: the parser should detect that boundary.
>>>PT markers with no colour at all on 21 Aug, legacy <font color> on 25 Aug. A message that _says_ "see my comments in blue" may contain no colour whatsoever — measured, twice.Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.