mail

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package mail provides a Mailer abstraction with an SMTP driver modelled on Laravel's Mail facade.

Two primitives:

  • Message — the email payload (From/To/Cc/Bcc/Subject/Text/HTML + attachments). Built by hand or via NewMessage(...).
  • Mailer — the transport interface. The bundled SMTPMailer talks to any RFC-compliant SMTP server (Postfix, Sendgrid, Mailgun, Postmark, AWS SES, Mailtrap, MailHog).

A LogMailer is shipped for tests/local dev — it captures sent messages in memory instead of delivering them.

Usage:

m := mail.NewSMTPMailer(mail.SMTPConfig{
    Host:     "smtp.mailgun.org",
    Port:     587,
    Username: "postmaster@example.com",
    Password: os.Getenv("MAIL_PASSWORD"),
    From:     "noreply@example.com",
})
err := m.Send(ctx, mail.NewMessage().
    To("user@example.com").
    Subject("Hello").
    HTML("<h1>Hi!</h1>").
    Build())

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyRecipients = errors.New("mail: at least one recipient required")

ErrEmptyRecipients is returned when a Message has no To/Cc/Bcc.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Filename    string
	ContentType string // e.g. "image/png"; auto-detected from extension when empty
	Data        []byte
}

Attachment is an inline file attached to a Message.

type Builder

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

Builder constructs a Message fluently.

func NewMessage

func NewMessage() *Builder

NewMessage returns an empty Builder.

func (*Builder) Attach

func (b *Builder) Attach(filename string, data []byte, contentType ...string) *Builder

Attach appends an attachment. ContentType may be empty — it's auto-detected from the filename extension.

func (*Builder) Bcc

func (b *Builder) Bcc(addrs ...string) *Builder

func (*Builder) Build

func (b *Builder) Build() Message

Build returns the finalised Message.

func (*Builder) Cc

func (b *Builder) Cc(addrs ...string) *Builder

func (*Builder) From

func (b *Builder) From(addr string) *Builder

func (*Builder) HTML

func (b *Builder) HTML(s string) *Builder

func (*Builder) Header

func (b *Builder) Header(k, v string) *Builder

func (*Builder) ReplyTo

func (b *Builder) ReplyTo(addr string) *Builder

func (*Builder) Subject

func (b *Builder) Subject(s string) *Builder

func (*Builder) Text

func (b *Builder) Text(s string) *Builder

func (*Builder) To

func (b *Builder) To(addrs ...string) *Builder

type LogMailer

type LogMailer struct {
	Sent []Message
	// contains filtered or unexported fields
}

LogMailer is an in-memory Mailer for tests/local dev. Sent messages accumulate in Sent.

func NewLogMailer

func NewLogMailer() *LogMailer

NewLogMailer returns an empty in-memory Mailer.

func (*LogMailer) Count

func (l *LogMailer) Count() int

Count returns how many messages have been sent.

func (*LogMailer) Last

func (l *LogMailer) Last() Message

Last returns the most recent message, or zero if none.

func (*LogMailer) Reset

func (l *LogMailer) Reset()

Reset clears the captured history.

func (*LogMailer) Send

func (l *LogMailer) Send(_ context.Context, msg Message) error

Send appends msg to Sent.

type Mailer

type Mailer interface {
	Send(ctx context.Context, msg Message) error
}

Mailer is the transport. Send delivers msg synchronously.

type Message

type Message struct {
	From        string
	To          []string
	Cc          []string
	Bcc         []string
	ReplyTo     string
	Subject     string
	Text        string
	HTML        string
	Headers     map[string]string
	Attachments []Attachment
}

Message is an immutable email payload built via Builder.

func (Message) Encode

func (m Message) Encode() ([]byte, error)

Encode renders the Message as a complete RFC 5322 email body (including headers + MIME multipart structure). The result is suitable for the DATA portion of an SMTP exchange.

Structure:

  • no attachments, text only → text/plain
  • no attachments, html only → text/html
  • no attachments, both → multipart/alternative
  • any attachments → multipart/mixed wrapping alternative-or-single body

func (Message) Recipients

func (m Message) Recipients() []string

Recipients returns every To/Cc/Bcc address (the envelope set).

type SMTPConfig

type SMTPConfig struct {
	Host     string // e.g. "smtp.mailgun.org"
	Port     int    // 25 / 465 / 587
	Username string
	Password string
	// From is the default From address used when Message.From is empty.
	From string
	// Insecure disables STARTTLS. Use only with isolated dev servers
	// like MailHog. The default (false) uses STARTTLS when offered.
	Insecure bool
	// TLSImplicit forces an immediate TLS handshake (port 465 mode).
	TLSImplicit bool
}

SMTPConfig configures an SMTP-backed Mailer.

type SMTPMailer

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

SMTPMailer talks plain-text SMTP with optional STARTTLS.

func NewSMTPMailer

func NewSMTPMailer(cfg SMTPConfig) *SMTPMailer

NewSMTPMailer returns a Mailer that talks to an SMTP server.

func (*SMTPMailer) Send

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

Send dispatches msg. If Message.From is empty, the config's default From is used. The wall-clock deadline is taken from ctx.

Jump to

Keyboard shortcuts

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