Documentation
¶
Overview ¶
Package mailer sends transactional email over SMTP or Resend using mail.* settings. When mail.host is unset it logs the message instead: in development (WithEnv("dev")) including the text body, so magic links still work; otherwise only the recipient and subject, with a warning.
Index ¶
- Constants
- func DecryptSecret(key []byte, enc string) (string, error)
- func EncryptSecret(key []byte, plain string) (string, error)
- type Attachment
- type Config
- type Mailer
- func (m *Mailer) Config(ctx context.Context) (Config, error)
- func (m *Mailer) Configured(ctx context.Context) bool
- func (m *Mailer) Key() []byte
- func (m *Mailer) Render(locale, name string, data any) (subject, html, text string, err error)
- func (m *Mailer) Send(ctx context.Context, to, subject, html, text string) error
- func (m *Mailer) SendMessage(ctx context.Context, msg Message) error
- func (m *Mailer) SendRequired(ctx context.Context, to, subject, html, text string) error
- func (m *Mailer) WithEnv(env string) *Mailer
- func (m *Mailer) WithKey(key []byte) *Mailer
- type Message
Constants ¶
const ( TLSStartTLS = "starttls" TLSImplicit = "tls" TLSNone = "none" )
TLS modes for mail.tls.
const ServiceName = "mailer"
ServiceName is the kernel service name the admin module registers the *Mailer under (kernel.Get[*mailer.Mailer](d, mailer.ServiceName)).
Variables ¶
This section is empty.
Functions ¶
func DecryptSecret ¶
DecryptSecret reverses EncryptSecret.
func EncryptSecret ¶
EncryptSecret encrypts plain with key (32 bytes, cfg.SecretKey). TODO(consolidate): migrate to db.Encrypt with fallback read (db.Encrypt is AAD-bound std base64 without the enc:v1: prefix; DecryptSecret must keep reading existing enc:v1: values until they are re-saved).
Types ¶
type Attachment ¶
Attachment is a file sent with a Message. ContentType defaults to application/octet-stream; Name is encoded per RFC 2231 when needed.
type Config ¶
type Config struct {
Host, Username, Password, From, FromName, TLS string
Port int
Provider, ResendAPIKey string
}
Config is the selected provider configuration; only its secret is decrypted.
type Mailer ¶
type Mailer struct {
// contains filtered or unexported fields
}
Mailer sends email. It is safe for concurrent use.
func New ¶
New returns a mailer reading SMTP settings from s. Call WithKey to enable decrypting mail.password.
func (*Mailer) Config ¶
Config reads mail.* settings and decrypts only the selected provider secret.
func (*Mailer) Configured ¶
Configured reports local settings presence, not provider reachability or delivery.
func (*Mailer) Key ¶
Key returns the secret key used for mail.password (for callers that store an encrypted password).
func (*Mailer) Render ¶
Render executes templates/<name>.<locale>.html (html/template) and templates/<name>.<locale>.txt (text/template; it must define "subject"), falling back to the English templates when the locale has none.
func (*Mailer) Send ¶
Send delivers one message to a single recipient. Without mail.host it returns nil after logging the message: in dev the text body too (e.g. a magic link) at INFO; in any other env only recipient and subject, at WARN, because a body can carry a sign-in link.
func (*Mailer) SendMessage ¶
SendMessage is Send with the optional headers of msg (Reply-To).
func (*Mailer) SendRequired ¶
SendRequired uses one configuration snapshot and never logs a message as a delivery fallback.
type Message ¶
type Message struct {
To, ReplyTo, Subject, HTML, Text string
Attachments []Attachment // optional; makes the message multipart/mixed
}
Message is one email for SendMessage. ReplyTo is optional (an address, optionally with a display name).