Documentation
¶
Overview ¶
Package email provides a lightweight, SMTP-first email toolkit for Go. It includes clean API, templates via fs.FS, multipart (text+HTML), attachments, inline images (CID), connection pooling, timeouts, retries with jitter, and optional rate limiting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backoff ¶
type Backoff interface {
// Next returns sleep before attempt i (0-based). ok=false when no more.
Next(i int) (d time.Duration, ok bool)
}
Backoff describes retry sleep schedule.
func ExponentialBackoff ¶
func ExponentialBackoff( attempts int, base time.Duration, max time.Duration, fullJitter bool, ) Backoff
ExponentialBackoff returns a Backoff with jitter. attempts is total tries (>=1). base is initial delay, max caps delay. fullJitter picks [0,d) vs half-jitter [d/2, d).
Parameters:
- attempts: The total tries.
- base: The initial delay.
- max: The max delay.
- fullJitter: The full jitter.
Returns:
- Backoff: The backoff.
type ConnPool ¶
type ConnPool struct {
MaxIdle int
IdleTTL time.Duration
New func() (any, error)
Close func(any) error
IsHealthy func(any) bool
// contains filtered or unexported fields
}
ConnPool is a simple sized pool for client connections. It stores opaque connections. Adapter owns the concrete type.
The pool is safe for concurrent use.
func NewConnPool ¶
func NewConnPool( maxIdle int, idleTTL time.Duration, newFn func() (any, error), closeFn func(any) error, isHealthyFn func(any) bool, ) *ConnPool
NewConnPool creates a new pool.
Parameters:
- maxIdle: The maximum number of idle connections.
- idleTTL: The idle timeout.
- newFn: The new function.
- closeFn: The close function.
- isHealthyFn: The is healthy function.
Returns:
- *ConnPool: The new pool.
func (*ConnPool) CloseAll ¶
func (p *ConnPool) CloseAll()
CloseAll drains the pool and closes all idle connections.
type Mailer ¶
type Mailer interface {
// Send sends an email message with the given options.
//
// Parameters:
// - ctx: The context for cancellation and timeouts.
// - msg: The email message to send.
// - opts: Optional configuration for this send operation.
//
// Returns:
// - error: An error if the email fails to send. Implementations
// should return context errors when the operation is cancelled
// or times out.
Send(ctx context.Context, msg types.Message, opts ...Option) error
}
Mailer defines the interface for email sending adapters. Implementations should handle connection management, authentication, and delivery according to their specific protocol (SMTP, API, etc.).
type Option ¶
type Option func(*SendConfig)
Option configures per-send behavior.
func WithDKIM ¶
func WithDKIM(cfg types.DKIMConfig) Option
WithDKIM enables DKIM signing using the provided config.
Parameters:
- cfg: The DKIM config.
Returns:
- Option: The option.
func WithHooks ¶
WithHooks attaches observability hooks (OTel-friendly, no deps).
Parameters:
- h: The hooks.
Returns:
- Option: The option.
func WithListUnsubscribe ¶
WithListUnsubscribe sets the List-Unsubscribe header.
Parameters:
- v: The List-Unsubscribe header value.
Returns:
- Option: The option.
func WithPool ¶
WithPool sets a connection pool to reuse adapter connections.
Parameters:
- pool: The connection pool.
Returns:
- Option: The option.
func WithRateLimit ¶
func WithRateLimit(bucket *TokenBucket) Option
WithRateLimit attaches a token bucket for throttling.
Parameters:
- bucket: The token bucket.
Returns:
- Option: The option.
type SendConfig ¶
type SendConfig struct {
ListUnsub string
Backoff Backoff
Rate *TokenBucket
Pool *ConnPool
Hooks *types.Hooks
DKIM *types.DKIMConfig
}
SendConfig is applied during Send.
type TemplateSet ¶
type TemplateSet struct {
// contains filtered or unexported fields
}
TemplateSet loads and renders text and HTML templates from an fs.FS.
Convention:
name.txt.tmpl -> plain text body name.html.tmpl -> HTML body
Both files are optional; at least one must exist to render a message.
func LoadTemplates ¶
func LoadTemplates(fsys fs.FS) (*TemplateSet, error)
LoadTemplates walks fsys and parses *.txt.tmpl and *.html.tmpl.
Parameters:
- fsys: The filesystem.
Returns:
- *TemplateSet: The template set.
- error: The error if the template set fails to load.
func MustLoadTemplates ¶
func MustLoadTemplates(fsys fs.FS) *TemplateSet
MustLoadTemplates panics on error; useful for init.
Parameters:
- fsys: The filesystem.
Returns:
- *TemplateSet: The template set.
func (*TemplateSet) Render ¶
Render renders "name" by locating "name.txt.tmpl" and "name.html.tmpl" anywhere in the parsed set. If only one exists, the other return is nil.
Parameters:
- name: The name of the template.
- data: The data to render the template with.
Returns:
- []byte: The plain text body.
- []byte: The HTML body.
- error: The error if the template fails to render.
type TokenBucket ¶
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket is a simple thread-safe token bucket.
func NewTokenBucket ¶
func NewTokenBucket(rate float64, burst int) *TokenBucket
NewTokenBucket returns a token bucket generating "rate" tokens per second with a capacity of "burst".
Parameters:
- rate: The rate of tokens per second.
- burst: The max tokens.
Returns:
- *TokenBucket: The token bucket.
func (*TokenBucket) Wait ¶
func (tb *TokenBucket) Wait()
Wait blocks until one token is available.
Parameters:
- tb: The token bucket.
Returns:
- void: The token bucket is blocked until one token is available.