mail

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mail provides transactional email with a fluent message builder, an SMTP driver, and a development log driver.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRecipients define package-level implementation state.
	ErrNoRecipients = errors.New("mail: message has no recipients")
	// ErrNoBody define package-level implementation state.
	ErrNoBody = errors.New("mail: message has no body")
	// ErrUnknownDriver define package-level implementation state.
	ErrUnknownDriver = errors.New("mail: unknown driver")
)

Functions

func Render

func Render(t *template.Template, name string, data any) (string, error)

Render executes the named template with data and returns the HTML.

Types

type AttachmentInfo

type AttachmentInfo struct {
	// Filename store data used by this type.
	Filename string `json:"filename"`
	// ContentType store data used by this type.
	ContentType string `json:"content_type"`
	// Size store data used by this type.
	Size int `json:"size"`
}

AttachmentInfo describes an attachment without exposing its content.

type Encryption

type Encryption string

Encryption selects the SMTP transport security.

const (
	// EncryptionNone define package-level implementation state.
	EncryptionNone Encryption = "none" // plain, port 25
	// EncryptionTLS define package-level implementation state.
	EncryptionTLS Encryption = "tls" // implicit TLS, port 465
	// EncryptionSTARTTLS define package-level implementation state.
	EncryptionSTARTTLS Encryption = "starttls" // mandatory STARTTLS, port 587
)

type Envelope

type Envelope struct {
	// From store data used by this type.
	From string `json:"from,omitempty"`
	// FromName store data used by this type.
	FromName string `json:"from_name,omitempty"`
	// To store data used by this type.
	To []string `json:"to,omitempty"`
	// Cc store data used by this type.
	Cc []string `json:"cc,omitempty"`
	// Bcc store data used by this type.
	Bcc []string `json:"bcc,omitempty"`
	// ReplyTo store data used by this type.
	ReplyTo string `json:"reply_to,omitempty"`
	// Subject store data used by this type.
	Subject string `json:"subject"`
	// Text store data used by this type.
	Text string `json:"text,omitempty"`
	// HTML store data used by this type.
	HTML string `json:"html,omitempty"`
	// Attachments store data used by this type.
	Attachments []AttachmentInfo `json:"attachments,omitempty"`
}

Envelope is a read-only snapshot of a message for inspection tooling such as the devtools mail outbox. Attachment content is never exposed, only its metadata.

type Mailer

type Mailer interface {
	// Send define an operation required by this interface.
	Send(ctx context.Context, message *Message) error
	// Close define an operation required by this interface.
	Close() error
}

Mailer sends messages.

func New

func New(options Options) (Mailer, error)

New builds a Mailer for the configured driver.

type Message

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

Message is a fluent mail builder. Builder errors (bad template, failed attachment read) are deferred and surface at Send.

func NewMessage

func NewMessage() *Message

NewMessage performs this package operation.

func (*Message) Attach

func (m *Message) Attach(filename, contentType string, r io.Reader) *Message

Attach adds an attachment read from r.

func (*Message) Bcc

func (m *Message) Bcc(addresses ...string) *Message

Bcc performs this package operation.

func (*Message) Cc

func (m *Message) Cc(addresses ...string) *Message

Cc performs this package operation.

func (*Message) Envelope

func (m *Message) Envelope() Envelope

Envelope snapshots the message. Slices are copied so later builder calls do not mutate the snapshot.

func (*Message) From

func (m *Message) From(address, name string) *Message

From overrides the mailer's default sender for this message.

func (*Message) HTML

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

HTML performs this package operation.

func (*Message) HTMLTemplate

func (m *Message) HTMLTemplate(t *template.Template, name string, data any) *Message

HTMLTemplate renders the named template with data as the HTML body.

func (*Message) ReplyTo

func (m *Message) ReplyTo(address string) *Message

ReplyTo performs this package operation.

func (*Message) Subject

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

Subject performs this package operation.

func (*Message) Text

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

Text performs this package operation.

func (*Message) To

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

To performs this package operation.

type Options

type Options struct {
	// Driver selects the transport: "log" (default, renders messages into
	// the logger) or "smtp".
	Driver string
	// Host store data used by this type.
	Host string
	Port int // defaults per encryption: 587 starttls, 465 tls, 25 none
	// Username store data used by this type.
	Username string
	// Password store data used by this type.
	Password   string
	Encryption Encryption // defaults to starttls
	// FromAddress is the default sender, required for the smtp driver.
	FromAddress string
	// FromName store data used by this type.
	FromName string
	Timeout  time.Duration // dial and send, defaults to 15s
	Logger   *slog.Logger  // log driver sink, defaults to slog.Default()
}

Options defines an implementation type used by this package.

Jump to

Keyboard shortcuts

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