Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Writing Gmail: Drafts, Replies, and Sending

Use gog to create drafts, send messages and replies, forward mail, change labels, archive messages, and move them to the trash. Each command is shown with its --dry-run --json plan, captured from gog v0.48.1 with an empty configuration, so you can see exactly what gog would ask Gmail to do. When a script or agent prepares a message, save it as a draft for someone to review in Gmail before sending.

The dry runs need no account. To reproduce them, prepare a throwaway home and 4 small files:

export GOG_HOME="$(mktemp -d)"
cd "$(mktemp -d)"
printf 'Hello,\n\nThe agenda for Monday is attached.\n' > note.txt
printf '<p>Hello,</p>\n<p>The agenda for Monday is attached.</p>\n' > note.html
printf 'Agenda\n1. Budget review\n2. Hiring plan\n' > agenda.txt
printf 'Thanks, all. I will bring the figures.\n' > reply.txt

MESSAGE_ID, THREAD_ID and DRAFT_ID stand for the identifiers that Reading Gmail and the draft commands print. Output, Schemas and Dry Runs explains the structure of a dry-run plan.

Why drafts come first

A draft is a saved, unsent message in the Drafts folder of the account. It can be opened, corrected and sent from Gmail on the web or on a phone, and deleting it affects no one but you. A send command delivers the message before it returns.

Confirmation requirements vary by command.

CommandAsks for confirmationEffect
gog gmail sendnodelivers the message at once
gog gmail drafts send DRAFT_IDnodelivers the draft at once
gog gmail trash MESSAGE_IDnomoves the message to the trash
gog gmail drafts delete DRAFT_IDyes, or --forcedeletes the draft permanently; drafts do not go to the trash
gog calendar deleteyes, or --forcesee Google Calendar

A send has no prompt to stop it, so the review must happen before the command runs (internal/cmd/confirm.go). Have the script or agent create a draft for review in Gmail.

Once you approve it, send it with the Send button or gog gmail drafts send. The --gmail-no-send flag allows draft commands and blocks every send command (see the last section and Safety Controls for Scripts and Agents).

Message bodies

All composing commands take the body from the same family of flags:

FlagBody
--body TEXTplain text from the command line
--body-file PATHplain text from a file; - reads standard input
--body-html HTMLan HTML part sent alongside the plain-text part
--body-html-file PATHHTML from a file; - reads standard input
--html-from-textbuilds an HTML part from the plain body

With only a plain body, gog sends text/plain. Gmail on the web displays such a message with a hard line break about every 70 characters.

--html-from-text (or GOG_GMAIL_HTML_FROM_TEXT=1) adds an HTML alternative built from the same text, which Gmail reflows to the window (docs/gmail-workflows.md). An explicit --body-html takes precedence over it.

Prefer --body-file for anything longer than a sentence: the text keeps its line breaks, the shell does not interpret quotes or $ signs inside it, and a dry run checks that the file exists. To send attachments without any text, pass an explicitly empty body, --body ''.

The signature that Gmail adds in the web interface is not added to messages created through the API. --signature appends the Gmail signature of the sending address, --signature-from ADDRESS the signature of another send-as alias (an additional sending address that Gmail has verified for the account), and --signature-file PATH a local file.

Drafts

Creating a draft

gog gmail drafts create takes the same recipient, subject, body and attachment flags as a send. --subject is required, and --attach can be repeated:

$ gog --account you@example.com --dry-run --json gmail drafts create --to friend@example.com --subject "Agenda for Monday" --body-file note.txt --attach agenda.txt
{
  "dry_run": true,
  "op": "gmail.drafts.create",
  "request": {
    "attachments": [
      "agenda.txt"
    ],
    "auto_from_addressed_alias": false,
    "bcc": null,
    "body_html_len": 0,
    "body_len": 43,
    "cc": null,
    "from": "",
    "quote": false,
    "reply_all": false,
    "reply_to": "",
    "reply_to_message_id": "",
    "signature": false,
    "signature_file": "",
    "signature_from": "",
    "subject": "Agenda for Monday",
    "thread_id": "",
    "to": [
      "friend@example.com"
    ]
  }
}

Without --dry-run, the JSON result of every draft-writing command has the keys draftId, message (the Gmail message inside the draft), threadId, inReplyTo, references and replyContextSource, plus attachments with filename and size in bytes when the draft has files (internal/cmd/gmail_drafts.go). Keep draftId to identify the draft in later commands.

Listing and reading drafts

gog --account you@example.com gmail drafts list
gog --account you@example.com gmail drafts list --all --json --results-only
gog --account you@example.com gmail drafts get DRAFT_ID
gog --account you@example.com gmail drafts get DRAFT_ID --format raw --plain > stored.eml
gog --account you@example.com gmail drafts get DRAFT_ID --download

drafts list returns 20 drafts per page by default and has the same --max, --page, --all and --fail-empty flags as the searches. Each item has id (the draft ID), messageId and threadId. drafts get --format raw prints the stored message, which lets you check the exact bytes before anyone sends it, and --download saves the draft’s attachments.

Updating a draft

gog gmail drafts update DRAFT_ID rewrites the draft. It requires --subject on every call, even when only the body changes:

$ gog --account you@example.com --dry-run --json gmail drafts update DRAFT_ID --body "x"
required: --subject
{"error":{"class":"usage","code":2,"message":"required: --subject"}}

Updating a draft retains recipients and attachments unless you change them explicitly. The plan shows what gog will keep with the *_keep_existing and preserve_attachments fields:

$ gog --account you@example.com --dry-run --json gmail drafts update DRAFT_ID --cc you@example.org --body-file note.txt --subject "Agenda for Monday" | jq -c '.request | {to, to_keep_existing, cc, cc_keep_existing, bcc_keep_existing, preserve_attachments}'
{"to":null,"to_keep_existing":true,"cc":["you@example.org"],"cc_keep_existing":false,"bcc_keep_existing":true,"preserve_attachments":true}

Pass --cc '' or --bcc '' to clear a field. --attach replaces all attachments, --clear-attachments removes them, and --clear-reply-context turns a reply draft into a standalone message by dropping its reply headers.

Sending and deleting a draft

gog gmail drafts send DRAFT_ID sends the draft as it is stored, without a prompt:

$ gog --account you@example.com --dry-run --json gmail drafts send DRAFT_ID
{
  "dry_run": true,
  "op": "gmail.drafts.send",
  "request": {
    "draft_id": "DRAFT_ID"
  }
}

gog gmail drafts delete DRAFT_ID removes a draft permanently. It asks for confirmation, and in a non-interactive run it refuses unless you add --force:

$ gog --account you@example.com --no-input gmail drafts delete DRAFT_ID
refusing to permanently delete gmail draft DRAFT_ID (not recoverable; drafts are not moved to Trash) without --force (non-interactive)

Sending a new message

gog gmail send (also available as the top-level shortcut gog send) composes and sends in 1 step. --to, --cc and --bcc take comma-separated lists, --subject is required for a new message, and --attach can be repeated:

$ gog --account you@example.com --dry-run --json gmail send --to friend@example.com --cc you@example.org --bcc you@example.com --subject "Agenda for Monday" --body-file note.txt --body-html-file note.html --attach agenda.txt
{
  "dry_run": true,
  "op": "gmail.send",
  "request": {
    "attachments": [
      "agenda.txt"
    ],
    "bcc": [
      "you@example.com"
    ],
    "body_html_len": 56,
    "body_len": 43,
    "cc": [
      "you@example.org"
    ],
    "from": "",
    "reply_all": false,
    "reply_to": "",
    "reply_to_message_id": "",
    "signature": false,
    "signature_file": "",
    "signature_from": "",
    "subject": "Agenda for Monday",
    "thread_id": "",
    "to": [
      "friend@example.com"
    ],
    "track": false,
    "track_split": false
  }
}

The plan lists both body parts by length and the attachment by path. It does not open the attachment, so a missing file is reported only by the real send. Other useful flags are --from ADDRESS, which sends from a send-as alias, and --reply-to ADDRESS, which sets the Reply-To header.

After a real send, the JSON result is an object with messageId and threadId of the new message, from, and attachments with filename and size when files were attached. When --track-split sends a separate copy to each recipient, the result is {"messages": [...]} with 1 such object per copy, each with its to (internal/cmd/gmail_compose.go).

Replying

The Gmail API has no reply operation of its own: a client has to fetch the original message, address the answer, copy the threading headers and quote the text. gog gmail reply and gog gmail reply-all do that work for you.

$ gog --account you@example.com --dry-run --json gmail reply MESSAGE_ID --body "Thanks, Monday at 10:00 works for me." | jq -c '.request | {message_id, quote, to_add, remove, subject_override}'
{"message_id":"MESSAGE_ID","quote":true,"to_add":null,"remove":null,"subject_override":""}

Their defaults follow Gmail’s own composer (docs/gmail-workflows.md):

  • The subject is the original subject with 1 Re: prefix.
  • The original message is quoted below the reply; --no-quote leaves it out. The plan shows this as "quote": true.
  • reply answers the Reply-To address when the original has one, otherwise the sender.
  • reply-all also keeps the original To and Cc recipients, except your own account and its send-as aliases.

The recipient flags edit the list gog derives from the original message. --to, --cc and --bcc add an address, or move an inherited address into that field, and --remove deletes an address from every field.

All 4 can be repeated. --subject overrides the subject, but a changed subject makes Gmail start a new conversation.

Reply drafts

gog gmail drafts reply and gog gmail drafts reply-all build exactly the same message and save it as a draft:

$ gog --account you@example.com --dry-run --json gmail drafts reply-all MESSAGE_ID --body-file reply.txt --remove you@example.org --no-quote | jq -c '{op} + (.request | {message_id, quote, remove})'
{"op":"gmail.drafts.reply-all","message_id":"MESSAGE_ID","quote":false,"remove":["you@example.org"]}

A dry run of a reply does not fetch the original message, so it cannot tell you the final recipient list. To verify the recipients before sending, open the draft in Gmail or inspect it with gog gmail drafts get DRAFT_ID.

Replying to a thread

reply and reply-all take a message ID. When you only have a thread ID, use --thread-id on gmail send or gmail drafts create. gog then replies to the latest message of the thread. Add --reply-all to derive the recipients and --quote to include the quoted text, which is off by default on these commands:

$ gog --account you@example.com --dry-run --json gmail drafts create --thread-id THREAD_ID --reply-all --quote --body-file reply.txt | jq -c '.request | {thread_id, reply_all, quote, to, subject}'
{"thread_id":"THREAD_ID","reply_all":true,"quote":true,"to":null,"subject":""}

--to and --subject are not required here because gog derives them from the thread, and the subject receives the Re: prefix (internal/cmd/gmail_drafts.go). The documentation describes --reply-to-message-id on gmail send as the lower-level way to compose a reply: an explicit --to or --cc replaces the derived recipients, and you must request quoting. For typical replies, the documentation recommends gmail reply and gmail reply-all.

Forwarding

gog gmail forward MESSAGE_ID forwards a message with a Fwd: subject, a forwarded-message block and the original attachments. --note (or --note-file, --note-html, --note-html-file) adds your text above the forwarded message, and --skip-attachments leaves the files out. gog gmail drafts forward saves the same message as a draft, and there --to is optional (docs/gmail-workflows.md):

$ gog --account you@example.com --dry-run --json gmail drafts forward MESSAGE_ID --to you@example.org --note "Forwarding for your records."
{
  "dry_run": true,
  "op": "gmail.drafts.forward",
  "request": {
    "bcc": null,
    "cc": null,
    "from": "",
    "message_id": "MESSAGE_ID",
    "note_html_len": 0,
    "note_len": 28,
    "signature": false,
    "signature_file": "",
    "signature_from": "",
    "skip_attachments": false,
    "to": [
      "you@example.org"
    ]
  }
}

Labels, archive, read state and trash

Gmail organizes mail with labels. Archiving removes INBOX, and marking a message as read removes UNREAD. gog offers both general label commands and named shortcuts (internal/cmd/gmail_archive.go).

CommandActs onLabel change in the plan
gog gmail labels modify THREAD_ID ... --add L --remove L1 or more threadsas given
gog gmail thread modify THREAD_ID --add L --remove Lall messages of 1 threadas given
gog gmail messages modify MESSAGE_ID --add L --remove L1 messageas given
gog gmail archive MESSAGE_ID ... (or --thread THREAD_ID)messages, or whole threadsremoves INBOX
gog gmail mark-read MESSAGE_ID ...messagesremoves UNREAD
gog gmail unread MESSAGE_ID ...messagesadds UNREAD
gog gmail trash MESSAGE_ID ...messagesadds TRASH, removes INBOX

--add and --remove accept label names or IDs, comma-separated or repeated. thread modify also has --add-label and --remove-label for a label name that itself contains a comma. The plans show the label changes directly:

$ gog --account you@example.com --dry-run --json gmail labels modify THREAD_ID --add Receipts --remove INBOX
{
  "dry_run": true,
  "op": "gmail.labels.modify",
  "request": {
    "add": [
      "Receipts"
    ],
    "remove": [
      "INBOX"
    ],
    "thread_ids": [
      "THREAD_ID"
    ]
  }
}
$ gog --account you@example.com --dry-run --json gmail archive --thread THREAD_ID
{
  "dry_run": true,
  "op": "gmail.archive",
  "request": {
    "action": "archived",
    "removed_labels": [
      "INBOX"
    ],
    "resource": "thread",
    "thread_ids": [
      "THREAD_ID"
    ]
  }
}

archive, mark-read, unread and trash can also select messages by query with --query (-q). --max caps the number of messages, 100 by default. Preview a query-based change before you run it, because it can touch many messages at once:

$ gog --account you@example.com --dry-run --json gmail mark-read --query 'is:unread label:Newsletters' --max 200
{
  "dry_run": true,
  "op": "gmail.read",
  "request": {
    "action": "marked as read",
    "added_labels": [],
    "max": 200,
    "message_ids": [],
    "query": "is:unread label:Newsletters",
    "removed_labels": [
      "UNREAD"
    ]
  }
}

The plan of a query-based command shows only the query. To see which messages it would touch, run the same query with gog gmail messages search first.

The trash

gog gmail trash moves messages to the trash by adding the TRASH label; it does not delete them permanently. It runs without a confirmation prompt:

$ gog --account you@example.com --dry-run --json gmail trash MESSAGE_ID
{
  "dry_run": true,
  "op": "gmail.trash",
  "request": {
    "action": "trashed",
    "added_labels": [
      "TRASH"
    ],
    "max": 100,
    "message_ids": [
      "MESSAGE_ID"
    ],
    "query": "",
    "removed_labels": [
      "INBOX"
    ]
  }
}

Permanent deletion is a separate command, gog gmail batch delete, which asks for confirmation and needs the broader https://mail.google.com/ OAuth scope, the permission that the account granted to gog at sign-in (see Authentication and Accounts). For ordinary cleanup, the gog documentation recommends the trash, which leaves room to recover from a mistake in Gmail.

A draft-first workflow

The following script saves a reply as a draft and prints the command that would send it. It passes --gmail-no-send, so even a mistake in the script cannot send mail, and it checks the dry-run plan before it writes anything:

#!/usr/bin/env bash
# Usage: draft-reply.sh MESSAGE_ID BODY_FILE
set -euo pipefail

message_id=$1
body_file=$2
common=(--no-input --gmail-no-send --json)

gog "${common[@]}" --dry-run gmail drafts reply "$message_id" \
  --body-file "$body_file" | jq -e '.op == "gmail.drafts.reply"' >/dev/null

draft_id=$(gog "${common[@]}" gmail drafts reply "$message_id" \
  --body-file "$body_file" | jq -er '.draftId')

echo "saved draft $draft_id"
echo "read it in Gmail, then send it with: gog gmail drafts send $draft_id"
GOG_ACCOUNT=you@example.com ./draft-reply.sh MESSAGE_ID reply.txt

jq -er prints the raw value and fails when draftId is missing, so the script never reports a draft that does not exist. The send itself stays a separate, deliberate step for a person.

What --gmail-no-send blocks

--gmail-no-send (or GOG_GMAIL_NO_SEND=1) refuses every Gmail send operation (internal/cmd/gmail_no_send.go), and it does so even in a dry run, so the plan already tells you that the command would be blocked:

$ gog --account you@example.com --dry-run --json --gmail-no-send gmail send --to friend@example.com --subject "Agenda for Monday" --body-file note.txt
Gmail sending is blocked by --gmail-no-send
{"error":{"class":"usage","code":2,"message":"Gmail sending is blocked by --gmail-no-send"}}
$ gog --account you@example.com --dry-run --json --gmail-no-send gmail drafts send DRAFT_ID
Gmail sending is blocked by --gmail-no-send
{"error":{"class":"usage","code":2,"message":"Gmail sending is blocked by --gmail-no-send"}}

Creating a draft is still allowed:

$ gog --account you@example.com --dry-run --json --gmail-no-send gmail drafts create --to friend@example.com --subject "Agenda for Monday" --body-file note.txt | jq -c '{dry_run, op}'
{"dry_run":true,"op":"gmail.drafts.create"}

To persist the send block for an account, use gog config no-send set. Safety Controls for Scripts and Agents explains how to combine it with command allowlists and --readonly.