gmail

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gmail implements the provider interfaces on the Gmail REST API (Google Workspace accounts), with OAuth auth code + PKCE login.

Index

Constants

View Source
const DefaultBaseURL = "https://gmail.googleapis.com"
View Source
const MaxMessageSize = 35 << 20

MaxMessageSize is Gmail's limit for a message including attachments (after base64 encoding) through the upload endpoint.

View Source
const (
	Name = "gmail"
)

Variables

View Source
var Endpoint = oauth2.Endpoint{
	AuthURL:   "https://accounts.google.com/o/oauth2/v2/auth",
	TokenURL:  "https://oauth2.googleapis.com/token",
	AuthStyle: oauth2.AuthStyleInParams,
}

Google OAuth endpoints. Declared here instead of importing x/oauth2/google, which pulls in the GCE metadata client.

Functions

func SaveClient

func SaveClient(store *auth.Store, alias string, c *Client) error

SaveClient stores the OAuth client for the account (0600).

func Scopes

func Scopes(m policy.Mode) []string

Scopes returns the scopes requested at login for a mode. Gmail has no scope that can create drafts without also being able to send (gmail.compose and gmail.modify both send), so draft and send share gmail.modify and only the policy gate stops a draft-mode account sending.

Types

type Client

type Client struct {
	ID     string `json:"client_id"`
	Secret string `json:"client_secret"`
}

Client is the OAuth client (GCP "Desktop app" type). Google calls the secret of an installed-app client non-confidential, but it is still kept out of account.json and out of Git.

func LoadClient

func LoadClient(store *auth.Store, alias string) (*Client, error)

LoadClient returns the stored OAuth client, or nil if there is none.

func LoadClientFile

func LoadClientFile(path string) (*Client, error)

LoadClientFile reads the JSON that the GCP console downloads for a Desktop OAuth client ({"installed": {"client_id": …, "client_secret": …}}).

type Config

type Config struct {
	Alias     string
	Username  string // locked mailbox address; used as From on drafts
	BaseURL   string // default DefaultBaseURL
	HTTP      *http.Client
	Tokens    TokenSource
	UserAgent string
}

type LoginParams

type LoginParams struct {
	Store     *auth.Store
	Account   *auth.Account // Alias, Mode set; Username set on re-login (lock)
	Client    *Client
	HTTP      *http.Client
	Endpoint  oauth2.Endpoint // default Endpoint
	BaseURL   string          // Gmail base, for the profile call
	UserAgent string

	Manual      bool
	Stdin       io.Reader
	OnPrompt    func(oauthflow.Prompt) error
	OpenBrowser func(string) error
}

type LoginResult

type LoginResult struct {
	Username string
	Scopes   []string
	Warnings []string
}

func Login

func Login(ctx context.Context, p LoginParams) (*LoginResult, error)

Login runs the browser flow (loopback or --manual), redeems the code with the PKCE verifier, checks the granted scopes, locks the account to the Gmail profile address and writes google-token.json.

type Provider

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

func New

func New(cfg Config) (*Provider, error)

func (*Provider) CreateDraft

func (p *Provider) CreateDraft(ctx context.Context, in provider.DraftInput) (*provider.Message, error)

func (*Provider) CreateForwardDraft

func (p *Provider) CreateForwardDraft(ctx context.Context, in provider.ForwardInput) (*provider.Message, error)

CreateForwardDraft quotes the original as a forwarded block and carries its (non-inline) attachments, in the original thread.

func (*Provider) CreateReplyDraft

func (p *Provider) CreateReplyDraft(ctx context.Context, in provider.ReplyInput) (*provider.Message, error)

CreateReplyDraft builds the reply locally (Gmail has no server-side createReply): In-Reply-To/References from the original, the same threadId, and the original quoted below the agent's text.

func (*Provider) DeleteDraft

func (p *Provider) DeleteDraft(ctx context.Context, id string) error

func (*Provider) Get

Get fetches one message. A draft id ("r…") fetches the draft. HTML is returned as HTML; the service derives text from it locally so hidden text is stripped.

func (*Provider) GetAttachment

func (p *Provider) GetAttachment(ctx context.Context, id, partID string) (*provider.Attachment, error)

GetAttachment fetches an attachment by MIME part id.

func (*Provider) List

List lists a label newest first.

func (*Provider) ListAttachments

func (p *Provider) ListAttachments(ctx context.Context, id string) ([]provider.AttachmentMeta, error)

func (*Provider) ListDrafts

func (p *Provider) ListDrafts(ctx context.Context, o provider.ListOptions) (*provider.Page, error)

ListDrafts lists drafts newest first; ids are draft ids.

func (*Provider) Mark

func (*Provider) Move

func (p *Provider) Move(ctx context.Context, id, dest string) (*provider.Message, error)

Move changes labels the way Gmail's "Move to" does: archive removes INBOX; spam and user labels replace INBOX; trash uses messages.trash.

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) Raw

Raw allows only the mailbox's own Gmail paths; the policy layer has already checked the method (and send-like paths) against the mode.

func (*Provider) Relabel

func (p *Provider) Relabel(ctx context.Context, id string, add, remove []string) (*provider.Message, error)

Relabel adds and removes labels. User labels are created on first use.

func (*Provider) Search

Search passes the query to Gmail's q= unchanged (same syntax as the Gmail search box). Spam and trash are excluded unless the query asks for them with in:spam / in:trash.

func (*Provider) Send

func (p *Provider) Send(ctx context.Context, id string) (*provider.SendResult, error)

Send sends an existing draft with drafts.send; Gmail deletes the draft and returns the sent message.

func (*Provider) Sync

Sync uses history.list from the stored historyId. The first run records the mailbox's current historyId and returns nothing. When Gmail no longer has the history (404: the id is too old), a new baseline is recorded.

func (*Provider) Thread

func (p *Provider) Thread(ctx context.Context, threadID string, limit int) ([]provider.Message, error)

Thread returns a thread oldest first.

func (*Provider) UpdateDraft

func (p *Provider) UpdateDraft(ctx context.Context, id string, u provider.DraftUpdate) (*provider.Message, error)

UpdateDraft rewrites the draft (Gmail replaces drafts whole). Fields not given are carried over from the current draft: recipients, subject, threading headers, the plain-text body and non-inline attachments. A draft written in Gmail with formatting keeps its text but loses the formatting and inline images.

type TokenSource

type TokenSource interface {
	Token(ctx context.Context) (string, error)
}

TokenSource returns a valid access token for the Gmail API.

func NewTokenSource

func NewTokenSource(p TokenSourceParams) (TokenSource, error)

NewTokenSource returns the TokenSource for a logged-in gmail account. Refreshed tokens are written back to google-token.json; after each refresh the mailbox is checked against the account lock.

func StaticToken

func StaticToken(tok string) TokenSource

StaticToken is a TokenSource for tests.

type TokenSourceParams

type TokenSourceParams struct {
	Store    *auth.Store
	Account  *auth.Account
	HTTP     *http.Client    // for token refresh and the identity check
	Endpoint oauth2.Endpoint // default Endpoint
	BaseURL  string          // Gmail base, for the identity check
}

Jump to

Keyboard shortcuts

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