Mail

Use the email CLI with your AI agent

Connect a mailbox with an app password, sync mail in parts, script the output and connect an agent.

This page explains how a command is built, and how an agent uses the tool. Read it before your first command. The references, ids and cursors in its examples are made up (me%40example.com, [email protected]): use the ones your own search, run or list gives.

The profile comes first

A profile is a name you choose for one account. It is the first word of every command that uses an account:

mail personal folders list

MAIL_PROFILE=personal does the same for one shell session. No profile is ever assumed, not even the only one: a command without one stops with exit code 2 and lists the profiles on this computer.

Connect a mailbox

First prepare the mailbox: turn on IMAP and make an app password. Prepare the mailbox has the steps for every provider, and session start prints them too.

mail personal session start --address [email protected] # new profile: finds the server, shows the steps, asks for the app password.
mail personal session start                 # an existing profile: asks for the app password again (checked, kept in the keyring)
mail personal account check                 # logs in and lists what the server supports
mail personal folders list                  # folders with role and counts
mail personal sync --since-time 2026-10-01  # reads new mail into the shared store; stored mail is skipped

session start on a new profile must find the mail server. It looks in its table of providers. Then it looks at the domain's MX record. Then it asks Thunderbird's list of mail servers and the domain's own settings file. If none knows the server, set it yourself with --host, --port and --security, or name a provider from the table: --provider yandex.

At a terminal, session start asks for what is missing: the provider and the address. Without a terminal it asks nothing. A missing --address or password is then an error, and it reads the password from MAIL_PASSWORD. The profile is saved only after the server accepts the app password. Outlook.com and Microsoft 365 accept only OAuth, so session start opens the browser for them (connect Outlook). To change the server of an existing profile, use config set or a new profile name.

session start takes the password from the terminal or from MAIL_PASSWORD. It never takes it from an option or a URL. After the server accepts it, mail keeps it in the OS keyring. If the keyring fails, mail keeps it in a private file and says so once on stderr. --credential-storage file chooses the file; we do not recommend it. If mail has no password, it reads the one Himalaya left (secret-tool, service himalaya, account the profile name). An existing Himalaya setup then needs no new password.

sync reads INBOX and the Sent folder; on Gmail it reads All Mail, since archived mail leaves INBOX. --folder or the profile's folders setting names other folders. It reads the newest mail first. --max-messages caps the new mail of one run. --rescan lists the whole window again. It saves text and headers, and the text of each attachment it can read, not the files. It reads attachments up to 10 MB each: the profile's read_attachments (false reads none) and max_attachment_mb change that, and --no-attachments, --attachments and --max-attachment-mb <n> change it for one run. A file left unread keeps its name and size. messages show of an email with such a file reads that email again from the server, every attachment whatever its size, and shows the text; --offline shows the stored email instead. To keep a file, use attachments download. The account's rules (mail personal rules, which memo keeps too) hold here too: mail a skip rule names is not read, and an envelope rule keeps only its sender, date and size.

A big mailbox does not fit in one run. sync has a budget of requests to the server, set by its deadline: 15 minutes at a terminal, 45 seconds over MCP. --timeout changes the deadline. --max-pages lowers the budget. The run saves what it read, then answers state: partial and gives the same command in next. Run it again to go on. mail personal status shows how far the sync is.

To keep a profile current, put its sync on a timer (systemd, launchd or Task Scheduler): mail personal schedule command mail-personal --every 5m -- sync. mail personal schedule list shows each schedule and how its last run went, and doctor says when a run keeps failing.

Yandex gives no message id. For such a server, sync lists only each message's number, flags, date and size, and it downloads each new message once. It skips stored mail by that number, so a rerun reads nothing twice. It does not notice mail deleted on the server or changed flags on mail it skips. --rescan does both. It also moves a stored reply that is outside its thread into the thread of the first email it answers, back to --since-time; rethreaded says how many it moved.

Sometimes the server cannot answer for one message (Yandex answers some with NO [UNAVAILABLE]). sync asks for it again at the end of the run. If it still fails, sync counts it in unreadable and leaves it for the next run.

Sometimes the server closes the connection. sync waits 1, 3, then 10 seconds, with a small random change so many runs do not retry together. It then reconnects. It checks that the server still numbers the folder's mail the same way, and goes on from the failed request while the budget and the deadline allow. If it still fails, the answer keeps the network error's own code as cause_code, and mail runs show has it.

Output for scripts

Every command except mcp serve on stdin and stdout takes --json, and prints one JSON value on stdout. Errors are JSON on stderr, with a code to branch on. The command reference lists the codes.

Agents

mail skill install puts the tool's skill where Claude Code, Codex, Gemini CLI and OpenClaw look for it. mail skill install --for hermes also adds that folder to Hermes Agent's own settings, so a bare skill install leaves Hermes out. mail personal mcp serve serves the profile's commands over MCP, on stdin and stdout:

claude mcp add mail -- mail personal mcp serve

The server offers every command. sync writes only to the local store; the messages and folders writes change the mailbox. The profile's permissions decide what runs; see settings.

For ChatGPT and Claude in the browser, mail personal mcp serve --http --public-url https://laptop.example.ts.net serves on 127.0.0.1 (port 8765, or --port) behind your own tunnel. A browser app logs in with a one-time code that the command prints; mail personal mcp revoke ends every login. mail personal mcp server start runs the same server in the background, mcp server status shows its port, the login code and the apps logged in, and mcp server stop ends it.

Review: what is new, what you owe, what waits

review reads the mail sync stored and asks the server nothing. It speaks tg's words:

mail personal review "date>2h"            # every email, yours too, in threads that changed in the last 2 hours
mail personal review --new                # what changed since the last `review --new`; 3 days back the first time
mail personal review --unanswered 1d      # threads whose newest email came to you (To or Cc) and has no answer for a day
mail personal review --waiting 7d         # threads where you wrote last and nobody answered for a week
mail personal review "bulk:false"         # leave out threads of mail sent to many, lists and envelopes

Every email is shown, mail sent to many too; each item's email says bulk, list_id and rule_id beside the sender's score, so you or an agent can sort. Mail newer than the last finished sync waits for the next review. --new keeps a point per mailbox and reader: yours, an agent's over MCP, and one per scheduled run, so a schedule never moves yours. Without a date, --unanswered and --waiting look at threads active in the last 30 days. memo tasks agenda --source email:[email protected] lists the same owed and waiting threads.

An agent or a pipeline that processes your mail keeps its own mark of how far it got, in the shared store, by the order mail was stored — so mail stored late, with an old date, is not skipped:

mail personal review --holder mail-triage --json        # what is stored past its mark; the answer gives a cursor
mail personal messages acknowledge --holder mail-triage --cursor 9123.1762090000000   # once processed: move the mark there
mail personal status                                    # how far each holder got, what it held and skipped

The mark only moves forward. Mail sent to many, envelopes and drafts are skipped and counted; an email still being read whole (partial) is held until it is, up to 7 days. A new holder starts from the mail stored now.

Search your mail

mail searches and reads the mail its profile synced, in the local store, and asks the mail server for mail sync has not read yet (--backend archive keeps to the store):

mail personal search messages invoice                                  # best match first, any form of the word
mail personal search messages 'from:[email protected] has:attachment date>30d'
mail personal search messages 'list:github.com is:unread' --newest     # a mailing list, newest first
mail personal messages show 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' # the whole email and its attachments' text
mail personal conversations list emails_count>1                        # threads, newest first
mail personal conversations show 'chat:email/me%40example.com/thread-1' # one thread, oldest first

The query language is the one every WireCat CLI uses (search guide): from:, to:, cc:, subject:, mailbox: or label:, list: (a List-Id or its domain), is:unread, has:attachment, filename:, content: (the text of attachments), size> and date:. Each command reads its profile's mailbox only; memo searches every mailbox together with your messages and notes (memo search mail, memo search all). The store keeps text, not the original message, so there is no raw source to show.

search messages also asks the mail server, so mail sync has not read yet is found too: Gmail with its own search over All Mail, other servers with IMAP SEARCH in INBOX, Sent and Archive (or the profile's folders). What the server finds is stored and the same query decides, so each hit says found_in: archive, server (new from the server) or both. --backend archive keeps to the store, --backend server answers only what the server found, and --server-time 20s waits longer than the default: 5 seconds, 20 on Yandex, whose search of a large INBOX takes 5 to 7 seconds. The wait always ends before the command's own deadline, so a slow server gives a partial answer, not a timeout. A search downloads only the first 256 KiB of each message the server finds: a bigger one is stored with its text and without its attachments, and the answer says so (search.server-partial). The next sync reads it whole and says how many in partial_emails (completed, left); one the server no longer gives where it was stored is given up on after three tries and counted in stuck, and stays readable without its attachments. The server reads words, from:, to:, cc:, subject:, body:, is: and date:; the rest of the query is applied to what it returns. IMAP SEARCH matches parts of words and no other forms of a word, so a server can miss a hit that the store, with every form, finds after sync. permissions.messages.server-search=deny in the profile turns the server search off.

Mail from a sender whose mail looks like spam or a scam comes after the rest of a page; --plain-order keeps the order the words give. messages show gives the first 4,000 characters of each attachment's text; --full gives all of it and the HTML body. --before-n and --after-n add the emails around it in its thread.

Export mail

The store keeps text, not the original message, so messages export reads the originals from the server again. It selects with the same query as search messages and writes a private folder (mode 700, files 600):

mail personal messages export 'from:[email protected]' --output-dir ~/mail-backup     # one mbox file per folder
mail personal messages export --folder INBOX --format maildir --output-dir ~/Maildir # Maildir++, for mu and notmuch
mail personal messages export 'date>30d' --max-messages 500 --output-dir ~/mail-backup --resume
mail personal stats messages top --by list --limit 10                          # which lists send the most

stats messages show counts the stored mail a query matches, received and sent, per calendar day, week (keyed by its Monday) or sender, in --timezone. Counts only, and it records no search:

mail personal stats messages show --by day --timezone Europe/Madrid -- 'date>=30d'
# {"item":{"by":"day","received":14,"sent":3,"rows":[{"key":"2026-11-01","name":null,"received":4,"sent":1}]}}
mail personal stats messages show --by sender --limit 10                       # by sender counts received mail only
  • What is written. Each message as the server holds it, byte for byte, in every folder it is filed in (Gmail files a message under All Mail and INBOX both: pass --folder to export one). mbox files are mboxrd: a body line that starts with the word From gets a > before it. Maildir file names carry the read (S) and starred (F) state.
  • Never over what is there. A folder with something in it is refused. --resume goes on with an export begun there (a journal file, .mail-export.jsonl, lists what is done and the length of each mbox file after it); --replace writes into any folder over files of the same name and deletes nothing.
  • In parts. A run stops at --max-messages, --max-pages or before its deadline and says state: "partial"; run it again with --resume. Mail the store has no UID for (sync again) or the server no longer has is counted, not written.

Look before you sync, and bring in an archive

account inspect reads the headers of the mailbox's mail from the server, stores nothing, and groups it by mailing list or sender, the largest group first, so you can decide what to keep before a big first sync:

mail gm account inspect                                    # every email the profile's folders hold
mail gm account inspect --since-time 2025-01-01 --limit 20
mail gm account inspect --domain-age                       # also asks when each sender's domain was registered
mail gm messages import ~/Takeout/Mail/archive.mbox         # a Google Takeout file of the same Gmail account
  • What a group shows. How many emails and MB, the share unread and sent as bulk, the years, whether you replied, a skip suggestion, and a score: how much the sender looks like spam, a scam or a robot. The kinds of mail a built-in rule kind would match close the list; rules add --kind <kind> --do envelope makes one a rule.
  • Sender domain age. A domain registered a few months before its mail arrived is a sign of fraud. --domain-age asks each shown group's domain registry over RDAP, one question a second, and keeps the answer in mail's state folder for good. sync scores every sender of the stored mail at most once a day, asks the registries about the domains heard from in the last two years that it does not know yet, and stops asking before its deadline; search ranks the mail of a sender with signs of fraud lower. Big mailbox providers and senders you replied to are never asked, and most country domains (.ru, .de, .es, .io) have no RDAP and stay unknown.
  • A Takeout archive. messages import reads a Gmail mbox file that Google Takeout wrote into this Gmail profile's stored mail. Takeout keeps Gmail's own ids, so an email both the file and sync bring is stored once. It only adds: Spam and Trash are left out, the profile's rules skip what they skip, and mail missing from the file is never marked deleted. An unchanged file read to the end is not read again; --max-messages and --since-time bound a run. A profile that is not Gmail is refused, because its ids would store every email twice.

Organise the mailbox

Every change goes to the server first and then into the store, so search messages shows it at once. The messages are named by the ref a search gives, or chosen by --filter with the same query:

mail personal messages mark-read 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E'                               # also mark-unread, star, unstar
mail personal messages update 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' --flag answered --unflag '$Junk'  # flags and keywords; --replace sets the whole set
mail personal messages move 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' --folder archive                   # a folder by path, name or role
mail personal messages copy --filter 'list:github.com' --folder Lists --yes
mail personal messages delete --filter 'from:[email protected] date<2025-01-01'   # a preview: nothing moves
mail personal messages delete --filter 'from:[email protected] date<2025-01-01' --yes
mail personal messages restore --run 20261101T101500Z-messages-delete-1a2b3c --yes
mail personal folders create Receipts                                  # also folders rename, folders delete
  • A preview first. A deletion, a change chosen by --filter, and a change of more than 20 messages answer with what would change and the same command with --yes in next; nothing changes until then. --dry-run always only previews. One message's flag or move goes ahead at once.
  • Undo. Each change answers with a run_id. Before it asks the server, the run records each message's place, flags and Gmail labels; messages restore --run <id> puts back what is still where the run left it: Trash back to its folder, a move moved back, a copy removed, flags as they were. Each write of a run is listed under writes with its operation_id; --write <id> undoes that write alone. The runs of memo's old cleanup command restore here too. Mail with text comes back to search with the next sync; an envelope at once.
  • Gmail. All mail is in All Mail and a folder is a label: move --folder Work adds the label Work and takes Inbox away, copy only adds it, --folder archive only takes Inbox away, and delete moves the mail from All Mail to Trash with its labels kept in the run, so a restore puts them back.
  • Trash. delete never expunges. It needs a Trash folder: the one the server marks \Trash, else the provider's own names (Yandex: «Удалённые»). A message already in Trash is counted as in_trash and left alone.
  • Rules. rules add --domain shop.example --do envelope keeps only the envelope of that mail; rules apply --yes cuts mail already stored. messages delete --rules then moves envelope mail to Trash, each email decided again: it stays when a protect or keep rule matches, you replied to the sender, it is starred, important, sent or a draft, it carries a label of yours, or it is under 30 days old; Gmail's labels and stars are read again just before the move. It refuses until the store backup is under 8 days old, and a first run moves at most 100.
  • Folders. folders delete is a preview until --yes, and refuses a folder that still holds mail, or whose count the server does not give; --allow-dangerous deletes it with its mail, for good (outside Gmail, the mail is gone). Only you pass --allow-dangerous, at a terminal: an agent over MCP never gets it. The inbox and the special folders keep their names.

Drafts

A draft goes into the mailbox's own Drafts folder, so it shows in every mail app. An agent writes it; you read it and send it from your mail app, or with drafts send.

mail personal drafts create --to [email protected] --subject 'Monday' 'Shall we meet at ten?'
mail personal drafts create --reply-to 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' --reply-all 'Thanks, agreed.'   # in the thread, to everyone but you
mail personal drafts create --to [email protected] --subject Notes - < notes.txt # - reads the text from stdin
mail personal drafts list                                                       # newest first, read from the server
mail personal drafts update [email protected] 'New text'                                   # only what you give changes
mail personal drafts delete [email protected] --allow-dangerous                             # the owner's; gone for good, a preview without it
  • The id is the draft's Message-ID, as drafts list shows it, with or without <>. It stays the same when the draft changes.
  • A reply reads the message from the server: it goes to its Reply-To or sender (with --reply-all also to everyone else it went to, never to you), with Re: and the In-Reply-To and References that keep it in the thread.
  • A change saves the new version first and then moves the old one to Trash, so a failure in between leaves two drafts, never none, and the old text can still be found. The text, --to, --cc, --bcc and --subject you give replace the old ones; the rest, the thread and the attachments stay. It needs a Trash folder.
  • Deleting is for good, so drafts delete takes --allow-dangerous, which only the owner passes: drafts delete and drafts send remove the draft with UID EXPUNGE, which needs UIDPLUS on the server.
  • The folder is the one the server marks \Drafts, else the provider's own name (Gmail «[Gmail]/Drafts», Yandex «Черновики»).
  • A draft is flagged \Draft and \Seen. drafts.delete asks first; drafts.create and drafts.update go ahead, and MAIL_READ_ONLY=1 refuses all three.

Your own tags

Tags of your own go on stored mail and stay on this computer: the server never sees them. They are the same tags memo reads, and a search finds them.

mail personal tags add invoices --message 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E'     # one email; --chat 'chat:email/me%40example.com/thread-1' tags a whole conversation
mail personal search messages 'tag:invoices date>30d'
mail personal tags list tag:invoices                  # what is tagged; type:message or type:chat
mail personal tags remove invoices --message 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E'

A tag is 1–32 letters a–z, digits and hyphens; upper case is lowered. What the server keeps instead are keywords (messages update --flag '$Work') and, on Gmail, labels (messages move|copy --folder Work).

Send, reply and forward

mail sends over the provider's SMTP server with the profile's app password; the server comes from the provider list, or from smtp_host, smtp_port and smtp_security in the profile.

mail personal messages send --to [email protected] --subject Lunch "See you at noon."
echo "The report." | mail personal messages send --to [email protected] --subject Report --file ./report.pdf
mail personal messages send --reply-to 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' "Paid today."                # Re:, In-Reply-To, References, the quote
mail personal messages send --reply-to 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' --reply-all "Thanks, all."   # also to the original's To and Cc, not you
mail personal messages forward 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' --to [email protected] "FYI"           # the original attached whole
mail personal messages send --to [email protected] --subject Hi "Hello" --dry-run   # build it, send nothing
mail personal messages send --to [email protected] --subject Digest --html < digest.html   # HTML, with a plain-text alternative made from it
mail personal drafts send [email protected]                                     # send a saved draft, then remove it
  • The question. Mail to anyone but you asks first, and shows from, to, cc, bcc, subject, each attachment and the first lines of the text; --yes answers it. With no terminal it answers confirmation_required. config set permissions.messages.send.others allow turns the question off. Mail only to yourself never asks.
  • Agents. Over MCP an agent may mail, reply, forward and send drafts only to you; mail to anyone else answers permission_error until you set messages.send.others in the config.
  • Files. --file takes a path as you name it; mail never expands a pattern. Credential folders (.ssh, keyrings, browser profiles) and the WireCat folders are refused unless --allow-any-file, which only you pass at a terminal: an agent over MCP never gets it. A text that says "attached" with no file draws a warning.
  • Sent. Gmail files mail sent over SMTP in Sent itself. Elsewhere mail appends its copy, with the Bcc line, unless Sent already holds that Message-ID. The message is then read into the store, so search messages finds it at once.
  • A lost answer. If the line breaks after the message left, the answer is outcome_unknown with an operation_id. Run the same command with --operation-id <id>: when Sent holds the message, nothing is sent again. On a provider that does not file sent mail itself the first try may still have gone; look in the recipient's mail before a retry.
  • Attachments. attachments list 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' numbers the files of one email; attachments download 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' --output-dir ./folder saves them, readable only by you, under their plain names; attachments download 'msg:email/me%40example.com/thread-1/%3Cid%40example.com%3E' 2 --output-dir ./folder saves only the second. The bytes come from the server and never go into the store. An existing file stays unless --replace.