Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Email sink

A thin SMTP relay. Delivers the Message via net/smtp.SendMail.

import (
    "github.com/frankbardon/parsec"
    "github.com/frankbardon/parsec/sinks/email"
)

es, _ := email.New(email.Config{
    Addr:     "smtp.example.com:587",
    From:     "parsec@example.com",
    Username: "parsec",
    Password: os.Getenv("SMTP_PASSWORD"),
})

p, _ := parsec.New(parsec.Options{})
p.Sinks().Register(es)

Once registered, you reference it by name in PublishOrSink:

delivered, err := p.PublishOrSink(ctx,
    "private:webapp.user.42.downloads",
    []byte(`{"status":"ready"}`),
    "email",
    email.Recipient{Address: "user@example.com"},
    parsec.Message{
        Subject: "Your download is ready",
        Body:    "Tap the link in the app.",
    })

Configuration

email.Config:

FieldRequiredNotes
AddryesSMTP host:port — e.g. smtp.example.com:587.
FromyesRFC 5322 mailbox-list, used as the envelope sender.
UsernamenoOmit for relays without auth (e.g. local Postfix).
PasswordnoRequired when Username is set.

If Username is set the sink uses smtp.PlainAuth over the connection. If not, the sink connects unauthenticated. The host component is parsed from Addr and used as the PlainAuth host identity.

Recipient

email.Recipient:

type Recipient struct {
    Address string
}

Address is required and must be an RFC 5322 mailbox. Empty addresses return PARSEC_INVALID_ARGUMENT.

Body shape

The sink stitches together a minimal RFC 5322 message:

From: <cfg.From>
To: <recipient.Address>
Subject: <message.Subject>

<message.Body>

Nothing else. No HTML wrapping, no MIME alternative, no templating — the publisher is responsible for whatever content shape the recipient expects. If you need richer email (HTML bodies, attachments, custom headers from Metadata), wrap this sink with your own and pre-format the body before calling Send.

Failure semantics

ConditionReturned code
Missing Addr or From at constructionPARSEC_SINK_CONFIG_INVALID
Wrong recipient typePARSEC_INVALID_ARGUMENT
Empty recipient addressPARSEC_INVALID_ARGUMENT
smtp.SendMail returns errorPARSEC_SINK_UNAVAILABLE

The sink does NOT retry. Network blips return immediately. If you need durability, wrap the sink in your own retry layer or queue the message before calling PublishOrSink.

See also