mail

package
v0.9.13 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 42 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 mail gives Outpost's apps email through one Gmail account, each at its own plus address: mail to you+yarr@gmail.com is Yarr's, and so is mail to you+k7q2@gmail.com when k7q2 is an alias Yarr was given. Outpost reads the inbox over IMAP, keeps each routed message for its app, labels it Outpost/<app> in Gmail and archives it. Mail to no route is never touched.

Apps don't hold the account's password. Each is started with OUTPOST_MAIL_URL, a loopback endpoint this package serves, and OUTPOST_MAIL_TOKEN, its own, and reads its messages and sends there. Each route also has an Atom feed, so a feed reader can subscribe to the mail sent to it. See docs/design/mail.md.

Index

Constants

View Source
const (
	EnvURL   = "OUTPOST_MAIL_URL"
	EnvToken = "OUTPOST_MAIL_TOKEN"
)

Environment variables every app is started with.

Variables

View Source
var (
	ErrNotAllowed = errors.New("this app isn't allowed to send mail; an admin can allow it under the app's Manage menu, in Mail")
	ErrNotReady   = errors.New("no Gmail account is set up; an admin sets one on the portal's Mail page")
	ErrLimit      = fmt.Errorf("this app has sent %d messages in the last 24 hours, its limit", dailyLimit)
)

Errors Send returns, which the endpoint turns into statuses.

Functions

This section is empty.

Types

type Addr

type Addr struct {
	Name    string `json:"name,omitempty"`
	Address string `json:"address"`
}

Addr is a name and address.

type Held

type Held struct {
	Count  int
	Newest time.Time
}

Held is what an app has waiting: how many messages, and when the newest came.

type Keys

type Keys interface {
	Value(name string) (string, bool)
	Put(name, value string) error
}

Keys is where the account's app password is kept: secrets.db, as a secret bound to no app.

type Message

type Message struct {
	ID int64 `json:"id"`
	// Route is the plus part it was sent to, and To that whole address.
	Route   string `json:"route"`
	To      string `json:"to"`
	From    Addr   `json:"from"`
	ReplyTo string `json:"reply_to,omitempty"`
	Subject string `json:"subject"`
	// MessageID is its Message-ID header, without the angle brackets.
	MessageID string    `json:"message_id,omitempty"`
	Date      time.Time `json:"date"`
	Received  time.Time `json:"received"`
	Text      string    `json:"text,omitempty"`
	HTML      string    `json:"html,omitempty"`
	// Attachments counts the parts that weren't kept: files, images.
	Attachments int `json:"attachments,omitempty"`
	// contains filtered or unexported fields
}

Message is one message routed to an app.

type Options

type Options struct {
	// Path is mail.db.
	Path string
	// Listen is the loopback address the endpoint serves on, which apps are
	// told as OUTPOST_MAIL_URL.
	Listen string
	// Address is config.toml's account, used until the portal sets one.
	Address string
	// Poll is how often the inbox is checked.
	Poll time.Duration
	// IMAP and SMTP are the servers, as host:port.
	IMAP, SMTP string
	// Keys holds the app password, under PasswordSecret.
	Keys           Keys
	PasswordSecret string
	// Services lists the declared apps, whose names are routes.
	Services func() []string
	// Logf receives one line per change and per routed message.
	Logf func(format string, v ...any)
	// Logger is where the service logs, at levels and with fields. It
	// supersedes Logf.
	Logger *log.Logger
}

Options configures a Service.

type Outgoing

type Outgoing struct {
	To  []string `json:"to"`
	Cc  []string `json:"cc,omitempty"`
	Bcc []string `json:"bcc,omitempty"`
	// Subject, Text and HTML; at least one of Text and HTML.
	Subject string `json:"subject"`
	Text    string `json:"text,omitempty"`
	HTML    string `json:"html,omitempty"`
	// FromName is the name shown beside the address; unset is the app's.
	FromName string `json:"from_name,omitempty"`
	// Route is which of the app's addresses it's from; unset is the one
	// named after the app.
	Route string `json:"route,omitempty"`
	// InReplyTo is the id of a message in the app's inbox this answers. It
	// threads the reply, and with no To, goes to whoever sent that one.
	InReplyTo int64 `json:"in_reply_to,omitempty"`
}

Outgoing is a message an app asks to send.

type Route

type Route struct {
	Route   string `json:"route"`
	Service string `json:"-"`
	// Alias says it's a random one, which can be removed; otherwise it's
	// the app's name.
	Alias bool   `json:"alias"`
	Note  string `json:"note,omitempty"`
	// Address is the whole address, "" until there's an account.
	Address string `json:"address,omitempty"`
	// Feed is the Atom feed of the mail sent to it.
	Feed string `json:"feed"`
}

Route is one address an app gets mail at.

type Service

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

Service is the mail endpoint, the inbox reader and their settings. It is safe for concurrent use.

func Open

func Open(opts Options) (*Service, error)

Open opens mail.db and readies the endpoint; Serve starts it, and Run reads the inbox.

func (*Service) AddAlias

func (s *Service) AddAlias(service, note, by string) (Route, error)

AddAlias gives service a new random address. The note says what it's for, such as the newsletter it was given to.

func (*Service) CanSend

func (s *Service) CanSend(service string) bool

CanSend reports whether service may send mail.

func (*Service) CheckNow

func (s *Service) CheckNow()

CheckNow asks Run to check the inbox without waiting for the next poll.

func (*Service) CheckWait

func (s *Service) CheckWait(ctx context.Context) error

CheckWait asks Run to check the inbox now, and waits until it has, or until ctx ends.

func (*Service) Close

func (s *Service) Close() error

Close closes mail.db.

func (*Service) Held

func (s *Service) Held() (map[string]Held, error)

Held is what each app has waiting. An app with nothing isn't in it.

func (*Service) Messages

func (s *Service) Messages(service string, after int64, limit int) ([]Message, error)

Messages lists service's messages after id, oldest first.

func (*Service) PasswordSecret

func (s *Service) PasswordSecret() string

PasswordSecret is the secret the app password is kept in.

func (*Service) Regenerate

func (s *Service) Regenerate(service, by string) error

Regenerate gives service a new token. The old one stops working at once.

func (*Service) RemoveAlias

func (s *Service) RemoveAlias(service, route, by string) error

RemoveAlias takes an alias away from service. Mail to it is then left in the inbox, like any other mail to no route.

func (*Service) Resolve

func (s *Service) Resolve(service string) (map[string]string, string)

Resolve gives an app its OUTPOST_MAIL_URL and OUTPOST_MAIL_TOKEN, and a revision that changes when its token does, so the supervisor restarts it with the new one.

func (*Service) Routes

func (s *Service) Routes(service string) []Route

Routes lists the addresses service gets mail at: its name, when that can be one, then its aliases, oldest first.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run checks the inbox every poll, and when CheckNow asks, until ctx ends.

func (*Service) Send

func (s *Service) Send(ctx context.Context, service string, o Outgoing) (string, error)

Send sends a message from one of service's addresses. It returns the new message's Message-ID.

func (*Service) Serve

func (s *Service) Serve(ctx context.Context, ln net.Listener) error

Serve answers apps on ln until ctx ends.

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the endpoint apps call, and the feeds feed readers fetch.

func (*Service) SetAccount

func (s *Service) SetAccount(address, password, by string) error

SetAccount sets the Gmail account. An empty password keeps the one saved. Gmail shows an app password in groups of four, so spaces are taken out.

func (*Service) SetSend

func (s *Service) SetSend(service string, allowed bool, by string) error

SetSend allows or stops service sending mail.

func (*Service) SetServices

func (s *Service) SetServices(f func() []string)

SetServices tells the Service how to list the declared apps, once the supervisor that knows them exists.

func (*Service) Status

func (s *Service) Status() Status

Status reports the account and how reading it is going.

func (*Service) TestAccount

func (s *Service) TestAccount(ctx context.Context, address, password string) error

TestAccount logs in to address with password, or with the saved one when password is empty, and logs out: what SetAccount would save, tried first.

func (*Service) URL

func (s *Service) URL() string

URL is what apps are told as OUTPOST_MAIL_URL.

type Status

type Status struct {
	Address    string `json:"address,omitempty"`
	FromPortal bool   `json:"from_portal,omitempty"`
	// HasPassword says the app password is saved.
	HasPassword bool `json:"has_password"`
	// Checked is the last check that finished, and Error why it failed.
	Checked time.Time `json:"checked,omitzero"`
	Error   string    `json:"error,omitempty"`
	// LastRouted is when a message was last routed to an app.
	LastRouted time.Time `json:"last_routed,omitzero"`
	// Routed counts messages routed since the daemon started.
	Routed int `json:"routed"`
}

Status is how reading the inbox is going.

func (Status) Ready

func (s Status) Ready() bool

Ready reports whether there's an account to read.

Jump to

Keyboard shortcuts

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