gmail

package
v1.0.56 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package gmail provides a client for the Gmail API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Filename     string `json:"filename"`
	MimeType     string `json:"mimeType"`
	Size         int64  `json:"size"`
	AttachmentID string `json:"attachmentId,omitempty"`
	PartID       string `json:"partId"`
	IsInline     bool   `json:"isInline"`
}

Attachment represents metadata about an email attachment

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps the Gmail API service

func NewClient

func NewClient(ctx context.Context) (*Client, error)

NewClient creates a new Gmail client with OAuth2 authentication

func (*Client) CreateDraft added in v1.0.41

func (c *Client) CreateDraft(ctx context.Context, msg DraftMessage) (*DraftResult, error)

CreateDraft assembles a MIME message and POSTs to users.drafts.create. The CLI never calls drafts.send — drafts sit in the user's Drafts folder for explicit human review.

func (*Client) DownloadAttachment

func (c *Client) DownloadAttachment(ctx context.Context, messageID string, attachmentID string) ([]byte, error)

DownloadAttachment downloads a single attachment by message ID and attachment ID

func (*Client) DownloadInlineAttachment

func (c *Client) DownloadInlineAttachment(ctx context.Context, messageID string, partID string) ([]byte, error)

DownloadInlineAttachment downloads an attachment that has inline data

func (*Client) FetchLabels

func (c *Client) FetchLabels(ctx context.Context) error

FetchLabels retrieves and caches all labels from the Gmail account

func (*Client) GetAttachments

func (c *Client) GetAttachments(ctx context.Context, messageID string) ([]*Attachment, error)

GetAttachments retrieves attachment metadata for a message

func (*Client) GetLabelID added in v1.0.35

func (c *Client) GetLabelID(ctx context.Context, name string) (string, error)

GetLabelID resolves a label display name to its ID. Calls FetchLabels if needed.

func (*Client) GetLabelName

func (c *Client) GetLabelName(labelID string) string

GetLabelName resolves a label ID to its display name

func (*Client) GetLabels

func (c *Client) GetLabels() []*gmail.Label

GetLabels returns all cached labels

func (*Client) GetMessage

func (c *Client) GetMessage(ctx context.Context, messageID string, includeBody bool) (*Message, error)

GetMessage retrieves a single message by ID

func (*Client) GetProfile added in v1.0.30

func (c *Client) GetProfile(ctx context.Context) (*Profile, error)

GetProfile retrieves the authenticated user's profile

func (*Client) GetThread

func (c *Client) GetThread(ctx context.Context, id string) ([]*Message, error)

GetThread retrieves all messages in a thread. The id parameter can be either a thread ID or a message ID. If a message ID is provided, the thread ID is resolved automatically.

func (*Client) ModifyMessages added in v1.0.35

func (c *Client) ModifyMessages(ctx context.Context, ids []string, addLabels, removeLabels []string) error

ModifyMessages modifies labels on one or more messages. Uses Messages.Modify for a single message; Messages.BatchModify for multiple. Gmail limits batch operations to 1000 IDs, so this method chunks automatically.

func (*Client) SearchMessageIDs added in v1.0.35

func (c *Client) SearchMessageIDs(ctx context.Context, query string, maxResults int64) ([]string, error)

SearchMessageIDs returns only message IDs matching the query (no metadata fetch). This is more efficient than SearchMessages when only IDs are needed. Note: returns a single page of results (up to ~100 when maxResults is 0). This matches SearchMessages behavior. For very large result sets, use a more specific query to narrow results.

func (*Client) SearchMessages

func (c *Client) SearchMessages(ctx context.Context, query string, maxResults int64) ([]*Message, int, error)

SearchMessages searches for messages matching the query. Returns messages, the count of messages that failed to fetch, and any error.

type DraftAttachment added in v1.0.41

type DraftAttachment struct {
	Filename string
	MimeType string
	Data     []byte
}

DraftAttachment is one attachment. Filename should be a basename; the gmail package re-applies filepath.Base defensively.

type DraftBodyKind added in v1.0.41

type DraftBodyKind int

DraftBodyKind selects the Content-Type for the body part.

const (
	// DraftBodyPlainText sends the body as text/plain; charset=UTF-8.
	// This is the zero value: a DraftMessage{} with an unset BodyKind
	// defaults to plain text, which is the safer interpretation when
	// callers construct DraftMessage directly without going through the CLI.
	DraftBodyPlainText DraftBodyKind = iota
	// DraftBodyHTML sends the body as text/html; charset=UTF-8.
	DraftBodyHTML
)

type DraftMessage added in v1.0.41

type DraftMessage struct {
	From        string   // optional; send-as alias
	To          []string // required; ≥1
	Cc          []string
	Bcc         []string
	Subject     string
	Body        []byte
	BodyKind    DraftBodyKind
	Attachments []DraftAttachment

	// ThreadID, when non-empty, threads the draft into an existing Gmail
	// conversation via Draft.Message.ThreadId. Empty = new thread.
	ThreadID string
	// InReplyTo, when non-empty, is emitted as the RFC 5322 In-Reply-To
	// header (typically the source message's Message-Id, brackets included).
	InReplyTo string
	// References, when non-empty, is emitted as the RFC 5322 References
	// header (space-joined Message-Ids per §3.6.4).
	References []string
}

DraftMessage describes a draft to be created. The command layer constructs this and hands it to (*Client).CreateDraft. The gmail package owns MIME assembly and the raw API call.

type DraftResult added in v1.0.41

type DraftResult struct {
	ID        string `json:"id"`
	MessageID string `json:"messageId"`
	ThreadID  string `json:"threadId,omitempty"`
}

DraftResult is the response after creating a draft.

type LabelResolver

type LabelResolver func(labelID string) string

LabelResolver is a function that resolves a label ID to its display name

type Message

type Message struct {
	ID       string `json:"id"`
	ThreadID string `json:"threadId"`
	Subject  string `json:"subject"`
	From     string `json:"from"`
	To       string `json:"to"`
	Date     string `json:"date"`
	Snippet  string `json:"snippet"`
	Body     string `json:"body,omitempty"`
	// BodyIsHTML reports that Body came from the message's text/html part
	// (no text/plain alternative). Internal routing bit for reply quoting;
	// intentionally excluded from the public --json output surface.
	BodyIsHTML  bool          `json:"-"`
	Attachments []*Attachment `json:"attachments,omitempty"`
	Labels      []string      `json:"labels,omitempty"`
	Categories  []string      `json:"categories,omitempty"`
	// Cc carries the raw "Cc" header value (comma-separated address list).
	Cc string `json:"cc,omitempty"`
	// RFCMessageID is the RFC 5322 Message-Id header, distinct from ID
	// (Gmail's internal message id). Used by reply derivation to populate
	// In-Reply-To and References on outgoing drafts.
	RFCMessageID string `json:"rfcMessageId,omitempty"`
	// References is the raw "References" header value (whitespace-separated
	// chain of Message-Ids).
	References string `json:"references,omitempty"`
	// InReplyTo is the raw "In-Reply-To" header value.
	InReplyTo string `json:"inReplyTo,omitempty"`
}

Message represents a simplified email message

type Profile added in v1.0.30

type Profile struct {
	EmailAddress  string
	MessagesTotal int64
	ThreadsTotal  int64
}

Profile represents a Gmail user profile.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL