Documentation
¶
Index ¶
Constants ¶
const ( SMTP = 1 MailGun = 2 )
const ( // DefaultConnectTimeout is how long to wait for an SMTP connection. DefaultConnectTimeout = 10 * time.Second // DefaultSendTimeout is how long to wait for a message to be transmitted // once connected. It also bounds the Mailgun API call. DefaultSendTimeout = 10 * time.Second // DefaultErrorChanGrace is how long a worker will wait to hand a result to // a Service.ErrorChan that nobody is currently reading, before abandoning // that result and returning to the pool. // // A worker is never blocked for longer than this. Previously the hand-off // was an unconditional channel send, so a result arriving after its caller // had stopped waiting parked that worker permanently; once every worker was // parked the dispatcher accepted mail forever without sending any of it. // Use SendAndWait to avoid the situation entirely. DefaultErrorChanGrace = 5 * time.Second )
Default timeouts. Each may be overridden per Service.
Variables ¶
var ( // ErrDispatcherClosed is returned by SendAndWait once Close has been called. ErrDispatcherClosed = errors.New("postal: dispatcher is closed") // ErrNotRunning is returned by SendAndWait when Run has not been called. // Without workers, a queued message would never be consumed. ErrNotRunning = errors.New("postal: dispatcher is not running; call Run first") )
Errors returned by the dispatcher.
Functions ¶
This section is empty.
Types ¶
type MailData ¶
type MailData struct {
ToName string // The name of the recipient.
ToAddress string // The email address of the recipient.
FromName string // The name of the sender.
FromAddress string // The email address of the sender.
ReplyTo string // The reply-to email address.
AdditionalTo []string // Additional TO recipients.
Subject string // The subject of the email message.
Content template.HTML // The content of the message, as HTML; sent verbatim when ContentType is "text/plain".
ContentType string // Optional: "text/html" (default), "text/plain" (Content sent as-is, no template), "application/xml", etc.
Template string // The template to use. If not specified, will use a simple default template.
CC []string // A slice of CC recipient emails.
BCC []string // A slice of BCC recipient emails.
Attachments []string // A slice of attachments, which must exist on disk (i.e. []string{"./files/myfile.pdf"}).
Data map[string]any // Data which is to be passed to the Go template.
InlineImages []string // A slice of images to be inlined in the email. PNG is preferred.
ServerURL string // The URL of the server (for backlinks in message).
}
MailData holds all information for a given message.
type MailDispatcher ¶
type MailDispatcher struct {
JobQueue chan MailProcessingJob // The channel we send work to.
ErrorChan chan error // Optional channel results are sent to.
// contains filtered or unexported fields
}
MailDispatcher is the main interface to this package. Calling New returns an instance of this type. Send mail with SendAndWait or Send.
All configuration and template state is held per dispatcher, so several dispatchers may be created and used concurrently in one process.
func New ¶
func New(s Service) (*MailDispatcher, error)
New returns a new mail dispatcher. Call Run to start its worker pool, then send mail with SendAndWait (synchronous, result returned to the caller) or Send (fire and forget, result delivered to Service.ErrorChan if set). Call Close when finished.
func (*MailDispatcher) Close ¶ added in v1.1.0
func (md *MailDispatcher) Close()
Close stops the dispatch loop and the worker pool, and waits for in-flight deliveries to finish. It is safe to call more than once. A dispatcher cannot be restarted after Close.
func (*MailDispatcher) Run ¶
func (md *MailDispatcher) Run()
Run runs the workers in our worker pool. It is safe to call more than once; subsequent calls are no-ops.
func (*MailDispatcher) Send ¶
func (md *MailDispatcher) Send(msg MailData)
Send takes a msg in postal.MailData format, wraps it in a postal.MailProcessingJob and sends it to the job queue for delivery.
Send is fire and forget: it returns as soon as the message is queued, and the result is delivered to Service.ErrorChan if one was configured. Because that channel is shared by every in-flight message, a result read from it cannot be attributed to any particular Send. Prefer SendAndWait when the caller needs to know whether this message was delivered.
func (*MailDispatcher) SendAndWait ¶ added in v1.1.0
func (md *MailDispatcher) SendAndWait(ctx context.Context, msg MailData) error
SendAndWait delivers one message and returns its result.
The result belongs to this message alone: the job carries a private buffered channel, so a worker never blocks handing the result back and no other caller can consume it. If ctx expires first, the worker still completes and reports without blocking, and ctx.Err() is returned here.
The context should allow at least Service.MaxSendDuration, or slow but successful sends will be abandoned and misreported as failures.
type MailMessage ¶
type MailMessage struct {
MailData MailData
}
MailMessage is a simple wrapper for MailData.
type MailProcessingJob ¶
type MailProcessingJob struct {
MailMessage MailData
// contains filtered or unexported fields
}
MailProcessingJob is the unit of work performed by a worker: one message to deliver, plus where to report the outcome.
type Service ¶
type Service struct {
Method int // How to send the message: postal.SMTP or postal.MailGun.
ServerURL string // The URL of the server mail is sent from.
SMTPServer string // The SMTP server.
SMTPPort int // The SMTP server's port.
SMTPUser string // The username for the SMTP server.
SMTPPassword string // The password for the SMTP server.
ErrorChan chan error // Optional channel for results (an error, or nil on success).
MaxWorkers int // Maximum number of workers in the pool.
MaxMessages int // How big the buffer should be for the JobQueue.
Domain string // The domain used to send mail.
APIKey string // The API key for mailgun.
SendingFromEU bool // If using mailgun and sending from EU, set to true.
TemplateDir string // Where templates are stored.
// ConnectTimeout bounds establishing the SMTP connection.
// Zero means DefaultConnectTimeout.
ConnectTimeout time.Duration
// SendTimeout bounds transmitting the message once connected, and bounds
// the Mailgun API call. Zero means DefaultSendTimeout.
SendTimeout time.Duration
// ErrorChanGrace bounds how long a worker waits to deliver a result to
// ErrorChan when no reader is ready. Zero means DefaultErrorChanGrace.
//
// Callers needing results delivered reliably should use SendAndWait, which
// carries a private result channel and never drops anything.
ErrorChanGrace time.Duration
}
Service is the type used to create a MailDispatcher.
A Service is copied into the MailDispatcher that New returns, and each dispatcher keeps its own configuration. Multiple dispatchers may be created and used concurrently within one process.
func (Service) MaxSendDuration ¶ added in v1.1.0
MaxSendDuration reports the longest a single delivery attempt can take, excluding template rendering. Callers imposing their own deadline on SendAndWait should keep it comfortably above this value, or slow but successful sends will be abandoned and misreported as failures.