mail

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package mail sends email, written as Svelte components in src/emails:

mailer := mail.New(mail.SMTP{Host: "smtp.example.com", Port: 587, Username: user, Password: pass}, "Acme <hello@acme.com>")

msg, err := mailer.Template(ctx, app, "Welcome", map[string]any{"name": u.Name, "link": confirmURL})
msg.To = []string{u.Email}
err = mailer.Send(ctx, msg)

Under tern dev, use mail.Log to print messages instead of sending them. To send in the background with retries, use Job with a jobs queue:

sendMail := mail.Job(q, mailer)
_, err = sendMail.Enqueue(ctx, *msg)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(msg *Message) ([]byte, []string, error)

Encode builds the MIME message and returns it with every recipient.

func Job

func Job(q *jobs.Queue, m *Mailer, opts ...jobs.HandleOption) *jobs.Kind[Message]

Job registers the "mail.send" job kind, which sends messages with m and retries failed deliveries (five attempts by default). Enqueue rendered messages on the returned kind.

Types

type Log

type Log struct {
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Log prints messages instead of sending them, for development. It keeps the last 50 for tests to inspect.

func (*Log) Send

func (l *Log) Send(_ context.Context, from string, to []string, raw []byte) error

Send implements Transport.

func (*Log) Sent

func (l *Log) Sent() []Sent

Sent returns the messages received so far.

type Mailer

type Mailer struct {
	// contains filtered or unexported fields
}

Mailer builds and sends messages.

func New

func New(t Transport, from string) *Mailer

New returns a mailer that sends with t from the default address from.

func (*Mailer) Send

func (m *Mailer) Send(ctx context.Context, msg *Message) error

Send delivers msg.

func (*Mailer) Template

func (m *Mailer) Template(ctx context.Context, app *kit.App, name string, props any) (*Message, error)

Template renders src/emails/<name>.svelte with props into a message, with the template's subject and HTML and a plain-text version.

type Message

type Message struct {
	From    string   `json:"from,omitempty"` // the Mailer's default when empty
	To      []string `json:"to"`
	Cc      []string `json:"cc,omitempty"`
	Bcc     []string `json:"bcc,omitempty"`
	ReplyTo string   `json:"replyTo,omitempty"`
	Subject string   `json:"subject"`
	HTML    string   `json:"html,omitempty"`
	Text    string   `json:"text,omitempty"`
	// Headers are extra headers, such as List-Unsubscribe.
	Headers map[string]string `json:"headers,omitempty"`
}

Message is an email.

type SMTP

type SMTP struct {
	Host     string
	Port     int
	Username string
	Password string
	// Insecure allows sending without TLS, such as to a local Mailpit.
	Insecure bool
	// Timeout bounds a delivery; the default is 30 seconds.
	Timeout time.Duration
}

SMTP sends through an SMTP server. Port 465 uses implicit TLS; other ports upgrade with STARTTLS when the server offers it, which is required unless Insecure is set (for local test servers).

func (SMTP) Send

func (s SMTP) Send(ctx context.Context, from string, to []string, raw []byte) error

Send implements Transport.

type Sent

type Sent struct {
	From string
	To   []string
	Raw  []byte
}

Sent is a message the Log transport received.

type Transport

type Transport interface {
	Send(ctx context.Context, from string, to []string, raw []byte) error
}

Transport delivers messages: SMTP, Log, or your own for an email API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL