Skip to main content

Gmail

Gmail Integration

GmailService → GmailAdapter → Google Gmail API v1

Google OAuth 2.0 · 23 actions · Threads · Messages · Labels · Attachments

Service Layer
GmailService
Action routing
Param validation
Error handling
calls
Adapter
GmailAdapter
HTTP client
OAuth token injection
RFC 2822 parsing
REST
External API
Google Gmail API
REST v1
googleapis.com
Bearer token auth
OAuth Scope Breakdown
gmail.readonly
Read threads, messages, labels
gmail.send
Send emails & drafts
gmail.modify
Trash, label, unlabel
gmail.labels
Create, update, delete labels

What is the Gmail Integration?

The Gmail integration gives Octopus full programmatic access to Google Gmail via the Gmail REST API v1. It covers the entire email lifecycle: reading and searching messages, managing threads, composing and sending email, working with drafts, organizing with labels, and downloading attachments.

Authentication uses Google OAuth 2.0 with granular scope control — only the permissions your workflow actually needs are requested at authorization time.

Key capabilities:

  • Thread & message management — list, search, get, trash, and delete conversations
  • Send & reply — compose new emails, reply to threads, forward messages
  • Drafts — create, list, and send drafts programmatically
  • Label organization — full CRUD for labels plus apply/remove on messages and threads
  • Attachments — list and download file attachments from messages
  • RFC 2822 raw headers — access raw Internet Message Format headers for advanced parsing

Gmail Data Model

Thread → Messages → Labels · Attachments

Thread
threadId (stable)
idstring — Gmail thread ID
snippetFirst message preview
historyIdChange tracking cursor
contains one or more
Message
messageId (unique per message)
idmessage ID
threadIdparent thread
labelIdsapplied labels
payloadMIME body + headers
internalDatereceived timestamp
rawRFC 2822 bytes (opt)
Labels
System: INBOX, SENT, TRASH, SPAM, DRAFT, UNREAD, STARRED
Custom: user-defined, color-coded
Attachments
Part of MIME payload tree
Referenced by attachmentId
Fetched separately via get_attachment
Pass --include-headers to get_message to receive the full RFC 2822 raw header block for advanced email parsing.

Methods Reference

All 23 available actions with descriptions and key parameters.

Threads

MethodDescriptionKey Parameters
list_threadsList threads in mailbox with optional filterquery, maxResults, pageToken, labelIds
get_threadFetch full thread with all messagesthread_id, format (full | metadata | minimal)
trash_threadMove thread to Trashthread_id
delete_threadPermanently delete thread (irreversible)thread_id
modify_threadAdd or remove labels on all thread messagesthread_id, addLabelIds, removeLabelIds

Messages

MethodDescriptionKey Parameters
list_messagesList message IDs in mailboxquery, maxResults, pageToken, labelIds
search_messagesSearch messages using Gmail query syntaxquery (e.g. from:boss@company.com is:unread)
get_messageFetch a single message by IDmessage_id, format, --include-headers (raw RFC 2822)

Drafts

MethodDescriptionKey Parameters
create_draftCreate a new draftto, subject, body, cc, bcc
list_draftsList all drafts in the accountmaxResults, pageToken
send_draftSend an existing draftdraft_id

Send

MethodDescriptionKey Parameters
send_emailCompose and send a new emailto, subject, body, cc, bcc, reply_to
reply_to_threadReply to an existing threadthread_id, body, to, cc
forward_messageForward a message to new recipientsmessage_id, to, body

Labels

MethodDescriptionKey Parameters
list_labelsList all labels (system + custom)
create_labelCreate a new custom labelname, messageListVisibility, labelListVisibility, color
update_labelUpdate an existing label's name or colorlabel_id, name, color
delete_labelDelete a custom labellabel_id

Label Operations

MethodDescriptionKey Parameters
label_messageApply one or more labels to a messagemessage_id, label_ids
unlabel_messageRemove one or more labels from a messagemessage_id, label_ids
label_threadApply labels to all messages in a threadthread_id, label_ids
unlabel_threadRemove labels from all messages in a threadthread_id, label_ids

Attachments

MethodDescriptionKey Parameters
list_attachmentsList all attachments on a messagemessage_id
get_attachmentDownload attachment data by IDmessage_id, attachment_id

CLI Reference

# Search for unread messages from a specific sender
constellation gmail search-messages --query "from:boss@company.com is:unread"

# Get a message with full raw RFC 2822 headers
constellation gmail get-message --message-id 18c3a9f2b8e12345 --include-headers

# List all threads in INBOX
constellation gmail list-threads --query "in:inbox" --max-results 20

# Send a new email
constellation gmail send-email \
--to user@example.com \
--subject "Hello from Octopus" \
--body "Automated message via Constellation."

# Reply to an existing thread
constellation gmail reply-to-thread \
--thread-id 18c3a9f2b8e12345 \
--body "Thanks, got it."

# Create a draft
constellation gmail create-draft \
--to user@example.com \
--subject "Draft subject" \
--body "Draft body content."

# Send an existing draft
constellation gmail send-draft --draft-id r8765432abcdef

# Create a custom label with color
constellation gmail create-label --name "Urgent" --color red

# Apply a label to a message
constellation gmail label-message \
--message-id 18c3a9f2b8e12345 \
--label-ids Label_12345

# Trash a thread
constellation gmail trash-thread --thread-id 18c3a9f2b8e12345

# List attachments on a message
constellation gmail list-attachments --message-id 18c3a9f2b8e12345

# Download an attachment
constellation gmail get-attachment \
--message-id 18c3a9f2b8e12345 \
--attachment-id ANGjdJ9qR3JVwE...

OAuth Scopes

Google OAuth 2.0 scopes are requested at authorization time. Only the scopes your workflow needs should be requested.

ScopeFull URIWhat It Enables
gmail.readonlyhttps://www.googleapis.com/auth/gmail.readonlyRead all threads, messages, labels, and drafts. No write access.
gmail.sendhttps://www.googleapis.com/auth/gmail.sendSend email and send existing drafts.
gmail.modifyhttps://www.googleapis.com/auth/gmail.modifyAll read + modify actions: trash, label/unlabel messages and threads. Does not grant permanent delete.
gmail.labelshttps://www.googleapis.com/auth/gmail.labelsCreate, update, and delete custom labels.

Least-privilege guidance: For read-only pipelines use gmail.readonly only. Add gmail.send only for outbound flows. gmail.modify is required for any label or trash operations. gmail.labels is only needed when managing the label taxonomy itself.


Error Handling

ErrorCauseResolution
401 UnauthorizedExpired or revoked OAuth tokenRe-authorize the integration via the OAuth flow
403 ForbiddenAction requires a scope not grantedRe-authorize with the required scope
404 Not FoundMessage ID, thread ID, or attachment ID does not existVerify IDs with a list call first
429 Too Many RequestsGmail API quota exceededImplement exponential backoff; check Google Cloud Console quotas
400 Bad RequestInvalid query syntax or missing required fieldCheck Gmail search syntax and required params