mailx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

mailx

Go Reference

github.com/go-mailx/mailx is a small Go library for sending email via swappable adapters. It provides a common MailerAdapter interface, a functional-options API for composing messages, and a Mailer that ties everything together.

Install

go get github.com/go-mailx/mailx

Usage

See the examples directory for working examples with each adapter.

Inline images

Images can be embedded in the mail and referenced from the HTML body by their Content-ID. Unlike data: URLs, this is rendered by Gmail web and mobile.

err := mailer.Send(ctx,
	mailx.To("user@example.com"),
	mailx.Subject("Welcome"),
	mailx.HtmlBody(`<img src="cid:logo" alt="Logo">`),
	mailx.Inline("logo", "image/png", logoPNG),
)

The Content-ID is given without angle brackets and must be unique within the mail. Inline attachments require an adapter whose MailInstance implements mailx.InlineAttachmentAdder; otherwise Send returns mailx.ErrInlineAttachmentsNotSupported instead of sending a mail with broken images.

API

See pkg.go.dev/github.com/go-mailx/mailx for all message options, MailerConfig, and FromAddressFunc built-ins.

Adapters

Module Transport
github.com/go-mailx/mailx-smtp SMTP via go-mail
github.com/go-mailx/mailx-ses AWS SES via aws-sdk-go-v2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInlineAttachmentsNotSupported is returned by Mailer.Send for a mail with
	// inline attachments if the adapter's MailInstance does not implement
	// InlineAttachmentAdder.
	ErrInlineAttachmentsNotSupported = errors.New("mailx: adapter does not support inline attachments")
	// ErrInvalidInlineAttachment is wrapped by the errors Mailer.Send returns for
	// an invalid inline attachment.
	ErrInvalidInlineAttachment = errors.New("mailx: invalid inline attachment")
)

Functions

This section is empty.

Types

type FromAddressFunc

type FromAddressFunc = func(ctx context.Context, mail Mail) string

func MailOverrideFromAddress

func MailOverrideFromAddress() FromAddressFunc

func StaticFromAddress

func StaticFromAddress(address string) FromAddressFunc

type InlineAttachment added in v1.1.0

type InlineAttachment struct {
	ContentID   string // referenced from HTML as "cid:<ContentID>", without angle brackets
	ContentType string // e.g. "image/png"
	Data        []byte
}

InlineAttachment is a file embedded in the mail, typically an image that the HTML body references by its Content-ID, e.g. <img src="cid:logo">.

type InlineAttachmentAdder added in v1.1.0

type InlineAttachmentAdder interface {
	AddInlineAttachment(InlineAttachment) error
}

InlineAttachmentAdder may be implemented by a MailInstance that supports inline attachments. Mailer.Send returns ErrInlineAttachmentsNotSupported for a mail with inline attachments if the instance does not implement it.

type Mail

type Mail struct {
	To           []string
	From         *mail.Address
	Bcc          []string
	ReplyTo      string
	Subject      string
	HtmlBodyFunc func() (string, error)
	TextBodyFunc func() (string, error)

	InlineAttachments []InlineAttachment
}

func New

func New(opts ...MailOpt) Mail

type MailInstance

type MailInstance interface {
	To([]string) error
	From(string) error
	Bcc([]string) error
	ReplyTo(string) error
	Subject(string) error
	HtmlBody(string) error
	TextBody(string) error
	Send(context.Context) error
}

type MailOpt

type MailOpt func(mail *Mail)

func Bcc

func Bcc(to ...string) MailOpt

func From

func From(from mail.Address) MailOpt

func HtmlBody

func HtmlBody(body string) MailOpt

func HtmlBodyTemplate

func HtmlBodyTemplate(tmpl *template.Template, data any) MailOpt

func Inline added in v1.1.0

func Inline(contentID, contentType string, data []byte) MailOpt

Inline embeds data in the mail so that the HTML body can reference it as "cid:<contentID>". The adapter must implement InlineAttachmentAdder.

func ReplyTo

func ReplyTo(to string) MailOpt

func Subject

func Subject(subject string) MailOpt

func TextBody

func TextBody(body string) MailOpt

func To

func To(to ...string) MailOpt

type Mailer

type Mailer struct {
	MailerAdapter
	Config *MailerConfig
}

func (*Mailer) Send

func (m *Mailer) Send(ctx context.Context, opts ...MailOpt) error

type MailerAdapter

type MailerAdapter interface {
	NewMail(context.Context) (MailInstance, error)
}

type MailerConfig

type MailerConfig struct {
	FromAddressSrc []FromAddressFunc
}

func (*MailerConfig) GetActualFromAddress

func (mc *MailerConfig) GetActualFromAddress(ctx context.Context, mail Mail) string

type Noop

type Noop struct{}

func (*Noop) AddInlineAttachment added in v1.1.0

func (n *Noop) AddInlineAttachment(InlineAttachment) error

func (*Noop) Bcc

func (n *Noop) Bcc([]string) error

func (*Noop) From

func (n *Noop) From(string) error

func (*Noop) HtmlBody

func (n *Noop) HtmlBody(string) error

func (*Noop) NewMail

func (n *Noop) NewMail(context.Context) (MailInstance, error)

func (*Noop) ReplyTo

func (n *Noop) ReplyTo(string) error

func (*Noop) Send

func (n *Noop) Send(context.Context) error

func (*Noop) Subject

func (n *Noop) Subject(string) error

func (*Noop) TextBody

func (n *Noop) TextBody(string) error

func (*Noop) To

func (n *Noop) To([]string) error

Jump to

Keyboard shortcuts

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