Documentation
¶
Overview ¶
Package onemailer sends transactional email over plain SMTP: password resets, verification links, invites. It speaks SMTP to any provider (Amazon SES, Postmark, SendGrid, Resend, …) and to a development catcher such as Mailpit, so going to production is a change of configuration only. See the README for the settings and the go-live checklist.
The pieces: LoadConfig reads {PREFIX}MAIL_* / {PREFIX}SMTP_* settings and refuses inconsistent ones; SMTPSender delivers one Message with the configured TLS mode (certificates always verified); Queue delivers off the request path with retries and logs outcomes without bodies, links or recipients; mailtest is an in-process SMTP server for tests.
Index ¶
Constants ¶
const ( ProviderNone = "none" ProviderSMTP = "smtp" )
Providers ({PREFIX}MAIL_PROVIDER).
const ( TLSStartTLS = "starttls" TLSImplicit = "tls" TLSNone = "none" )
TLS modes ({PREFIX}SMTP_TLS).
Variables ¶
DefaultDelays is the retry schedule: at once, then after 10 s, 1 min and 5 min.
var ErrHeaderInjection = errors.New("onemailer: a header value contains a line break")
ErrHeaderInjection is returned for a header value holding CR or LF.
Functions ¶
func IsLoopback ¶
IsLoopback reports whether host is localhost or a loopback IP address.
Types ¶
type Config ¶
type Config struct {
Provider string
PublicURL string // the web origin links point at; "" when unset
From *mail.Address
ReplyTo *mail.Address // optional ({PREFIX}MAIL_REPLY_TO)
Host string
Port int
TLS string
Username string
// AllowPlainHost is the one non-loopback host that may receive plain
// SMTP ({PREFIX}SMTP_ALLOW_PLAIN_HOST): a development catcher on a
// container network, such as Mailpit at "mailpit".
AllowPlainHost string
// Prefix is the environment-variable prefix the configuration was
// loaded with (for example "LIBRARYZ_"); errors name variables with it.
Prefix string
// contains filtered or unexported fields
}
Config is the mail configuration. Its String method never prints the password.
func LoadConfig ¶
LoadConfig reads the mail settings from the environment, each variable named with prefix (for example "LIBRARYZ_"):
{P}MAIL_PROVIDER none (default) or smtp
{P}PUBLIC_URL absolute http(s) origin links point at; required with smtp;
http only to localhost or when {P}ENV=development
{P}MAIL_FROM sender, "App <accounts@example.org>"; required with smtp
{P}MAIL_REPLY_TO optional reply address
{P}SMTP_HOST required with smtp
{P}SMTP_PORT default 587
{P}SMTP_TLS starttls (default), tls (implicit, usually 465) or none
{P}SMTP_ALLOW_PLAIN_HOST the one non-loopback host allowed with none (dev catcher)
{P}SMTP_USERNAME login, when the server requires one
{P}SMTP_PASSWORD or {P}SMTP_PASSWORD_FILE (preferred); at most one
Every error names its variable; none contains the password.
type Message ¶
type Message struct {
Kind string
To string
Subject string
Text string
HTML string // optional; with it the message is multipart/alternative
Attrs []slog.Attr
}
Message is one email. Kind names it in logs ("password_reset"); Attrs are logged with its outcome and must never hold a secret (no token, no link, no body).
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue delivers messages outside the request that produced them (so a slow server never stalls a request): a bounded in-process queue, one worker, and retries on a fixed schedule. A message that cannot be queued or delivered is dropped and logged; the user can ask again.
func NewQueue ¶
func NewQueue(sender Sender, cfg QueueConfig) *Queue
NewQueue starts a queue in front of sender.
type QueueConfig ¶
type QueueConfig struct {
Capacity int // default 256
Delays []time.Duration // before each try; default 0, 10 s, 1 min, 5 min
Logger *slog.Logger // default slog.Default()
// After replaces time.After in tests.
After func(time.Duration) <-chan time.Time
}
QueueConfig tunes a Queue; the zero value gives the defaults.
type SMTPSender ¶
type SMTPSender struct {
// contains filtered or unexported fields
}
SMTPSender delivers over SMTP with the configured TLS mode. It verifies certificates against the host name and never skips verification.
func NewSMTPSender ¶
func NewSMTPSender(cfg Config) (*SMTPSender, error)
NewSMTPSender returns a sender for an smtp configuration.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mailtest is an in-process SMTP server for tests of internal/mail and its users: plain, STARTTLS or implicit TLS, optional AUTH PLAIN, and scripted failures.
|
Package mailtest is an in-process SMTP server for tests of internal/mail and its users: plain, STARTTLS or implicit TLS, optional AUTH PLAIN, and scripted failures. |