Help us shape the future of Go! Take the 2026 Go Developer Survey

mail

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package mail provides a fluent email API backed by go-mail (SMTP). HTML templates are rendered from Go's html/template package. Attachments, CC/BCC, and inline images are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send(ctx context.Context, m *Mailer, to, subject, tmpl string, data any) error

Send is a convenience one-liner.

mail.Send(ctx, mailer, "user@example.com", "Welcome!", "welcome.html", data)

Types

type Config

type Config struct {
	Driver     string // "smtp" | "log"  (default: "smtp")
	Host       string
	Port       int
	Username   string
	Password   string
	FromName   string
	FromAddr   string
	Encryption string // "tls", "starttls", "none"  (default: "tls")
	Timeout    time.Duration
}

Config holds mail settings.

type LogTransport

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

LogTransport writes emails to a log file instead of sending them.

func (*LogTransport) Send

func (l *LogTransport) Send(msg *Message) error

type Mailer

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

Mailer manages message construction and delivery.

func New

func New(cfg Config, logger ...*slog.Logger) *Mailer

New creates a Mailer. Templates are loaded lazily via LoadTemplates.

func NewFromConfig

func NewFromConfig(cfg *config.Config, logger ...*slog.Logger) *Mailer

NewFromConfig creates a Mailer from oni.Config, reading all mail.* keys.

func (*Mailer) LoadTemplates

func (m *Mailer) LoadTemplates(dir string) error

LoadTemplates parses all *.html files in dir into the template set. Template names are relative paths, e.g. "welcome.html".

func (*Mailer) NewMessage

func (m *Mailer) NewMessage(ctx context.Context) *Message

NewMessage creates a fluent message builder for ctx.

type Message

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

Message is a fluent email message.

func (*Message) Attach

func (m *Message) Attach(path string) *Message

Attach adds a file attachment by path. If the file cannot be opened the error is recorded and returned by Send() — the mail is never silently sent without its attachment. The file is closed after Send.

func (*Message) AttachReader

func (m *Message) AttachReader(name string, r io.Reader) *Message

AttachReader adds an attachment from an io.Reader.

func (*Message) BCC

func (m *Message) BCC(addrs ...string) *Message

BCC adds BCC recipients.

func (*Message) CC

func (m *Message) CC(addrs ...string) *Message

CC adds CC recipients.

func (*Message) Err

func (m *Message) Err() error

Err returns the first error recorded while building the message, if any.

func (*Message) From

func (m *Message) From(addr string) *Message

From overrides the default sender address.

func (*Message) HTML

func (m *Message) HTML(body string) *Message

HTML sets a raw HTML body.

func (*Message) Header

func (m *Message) Header(key, value string) *Message

Header adds a custom email header. Keys or values containing CR/LF are rejected (recorded as an error returned by Send) to block header injection.

func (*Message) Send

func (m *Message) Send() error

Send delivers the message via the configured transport. Any error recorded while building the message (template failure, unreadable attachment, invalid header) aborts the send and is returned instead.

func (*Message) Subject

func (m *Message) Subject(s string) *Message

Subject sets the email subject.

func (*Message) Text

func (m *Message) Text(body string) *Message

Text sets a plain-text body.

func (*Message) To

func (m *Message) To(addrs ...string) *Message

To sets the recipient address(es).

func (*Message) View

func (m *Message) View(tmplName string, data any) *Message

View renders a named HTML template with data as the HTML body.

mailer.NewMessage(ctx).To("user@example.com").Subject("Welcome!").
    View("welcome.html", map[string]any{"Name": "Alice"}).
    Send()

type SMTPTransport

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

SMTPTransport delivers via SMTP using go-mail.

func (*SMTPTransport) Send

func (s *SMTPTransport) Send(msg *Message) error

type Transport

type Transport interface {
	Send(msg *Message) error
}

Transport is the delivery backend used by a Mailer.

Jump to

Keyboard shortcuts

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