Documentation
¶
Overview ¶
Package mailer sends email through pluggable providers (Amazon SES, Mailgun) behind a single Message model and a one-method Sender interface.
Provider failures surface as typed errors you can match with errors.Is (ErrAuth, ErrRateLimited, ErrRejected, ...) regardless of which provider produced them. Retry and Failover wrap any Sender, so "try SES, fall back to Mailgun" is a one-liner instead of application code.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidMessage = &Error{Kind: KindInvalid, Message: "invalid message"} ErrAuth = &Error{Kind: KindAuth, Message: "authentication failed"} ErrRateLimited = &Error{Kind: KindRateLimited, Message: "rate limited", Retryable: true} ErrRejected = &Error{Kind: KindRejected, Message: "message rejected"} ErrProviderDown = &Error{Kind: KindProviderDown, Message: "provider unavailable", Retryable: true} )
Sentinel errors for matching with errors.Is.
Functions ¶
func IsRetryable ¶
IsRetryable reports whether err is a provider error worth retrying. Retry uses it; it is exported because application-level queues want the same decision.
Types ¶
type Attachment ¶
Attachment is an in-memory file attached to a Message. ContentType falls back to application/octet-stream when empty.
type Error ¶
Error is a send failure. Provider is "mailgun" or "ses"; Code carries the raw provider signal (HTTP status or AWS error code). Match families with errors.Is (e.g. errors.Is(err, mailer.ErrRateLimited)) and read the concrete details with errors.As.
type Kind ¶
type Kind int
Kind groups provider errors into families so callers can match behavior without caring which provider produced the error.
type Message ¶
type Message struct {
From string
To []string
Cc []string
Bcc []string
ReplyTo string
Subject string
Text string
HTML string
Attachments []Attachment
// Headers are extra message headers (e.g. "X-Campaign-ID"). Providers that
// cannot send a header transparently switch to their raw/MIME path.
Headers map[string]string
// Tags map to Mailgun o:tag values and SES message tags. SES restricts
// tag names to [a-zA-Z0-9_-].
Tags []string
}
Message is a provider-independent email. Text or HTML (or both) must be set; everything else is optional. Addresses accept both bare ("a@example.com") and display-name ("Ayşe <a@example.com>") forms.
type Result ¶
Result reports a successful send. MessageID is the provider-assigned id and its format differs per provider; Provider tells which sender actually delivered (useful behind Failover).
type RetryOption ¶
type RetryOption func(*retrySender)
RetryOption configures Retry.
func WithAttempts ¶
func WithAttempts(n int) RetryOption
WithAttempts sets the total number of attempts (default 3).
func WithBaseDelay ¶
func WithBaseDelay(d time.Duration) RetryOption
WithBaseDelay sets the first backoff delay (default 500ms). Each retry doubles it up to the max delay.
func WithMaxDelay ¶
func WithMaxDelay(d time.Duration) RetryOption
WithMaxDelay caps the backoff delay (default 10s).
type Sender ¶
Sender sends a single message. Implementations: mailgun.Client, ses.Client, and the Retry / Failover wrappers.
func Failover ¶
Failover tries senders in order and returns the first success. It moves on for any failure except an invalid message, which would fail on every provider anyway. All collected errors are joined, so errors.Is still matches families on the combined error.
Combine with Retry per provider when you want "retry primary a few times, then switch": Failover(Retry(primary), fallback).
func Retry ¶
func Retry(next Sender, opts ...RetryOption) Sender
Retry wraps next so retryable errors (see IsRetryable) are retried with exponential backoff and full jitter. Non-retryable errors return immediately. Note that the SES provider already retries throttling at the AWS SDK layer; stacking Retry on top multiplies attempts.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
send
command
Sends one message through Mailgun, falling back to Amazon SES when Mailgun is unreachable.
|
Sends one message through Mailgun, falling back to Amazon SES when Mailgun is unreachable. |
|
Package mailgun implements mailer.Sender on the Mailgun HTTP API (v3) with no dependency beyond the standard library.
|
Package mailgun implements mailer.Sender on the Mailgun HTTP API (v3) with no dependency beyond the standard library. |
|
Package ses implements mailer.Sender on Amazon SES v2 using the official AWS SDK.
|
Package ses implements mailer.Sender on Amazon SES v2 using the official AWS SDK. |