mailer

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 12 Imported by: 0

README

go-mailer

Mailer wrapper for Go that supports unit testing.

GitHub tag (latest SemVer) PkgGoDev

Mailers supported:

  1. SendGrid
  2. MailGun

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidType = errors.New("invalid mailer type")

ErrInvalidType is returned when an invalid mailer type is provided.

Functions

func IsDummyMode

func IsDummyMode(ctx context.Context) bool

IsDummyMode returns true if dummy mode is enabled.

func WithDummyMode

func WithDummyMode(ctx context.Context) context.Context

WithDummyMode returns a new context with dummy mode enabled.

Types

type Config

type Config struct {
	APIKey         string                // The API key for the mailer.
	Domain         string                // The domain for the mailer (only used for Mailgun).
	Retry          bool                  // Whether to retry sending the mail.
	BackoffOptions []backoff.RetryOption // The backoff options for the mailer.  If Retry is true, then these options will be used to retry the send.
}

Config is the configuration for the mailer.

type DummyMailServer

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

DummyMailServer is our "dummy" mail server. It is indexed by the API key and contains a list of messages in ascending time order.

func Dummy

func Dummy() *DummyMailServer

Dummy returns the dummy mail server.

This is a singleton instance of the dummy mail server.

func (*DummyMailServer) APIKeys added in v0.1.1

func (d *DummyMailServer) APIKeys() []string

APIKeys returns the API keys used by the dummy mail server so far.

func (*DummyMailServer) Addresses added in v0.1.1

func (d *DummyMailServer) Addresses(apiKey string) []string

Addresses returns the list of e-mail addresses available for the given API key.

func (*DummyMailServer) Inbox

func (d *DummyMailServer) Inbox(apiKey string, address string) []*Message

Inbox returns the messages in the inbox for the given API key and address.

func (*DummyMailServer) LastMessageInInbox

func (d *DummyMailServer) LastMessageInInbox(apiKey string, address string) *Message

LastMessageInInbox returns the last message in the inbox for the given API key and address. If there are no messages in the inbox, it returns nil.

func (*DummyMailServer) LastMessageInOutbox

func (d *DummyMailServer) LastMessageInOutbox(apiKey string, address string) *Message

LastMessageInOutbox returns the last message in the outbox for the given API key and address. If there are no messages in the outbox, it returns nil.

func (*DummyMailServer) Outbox

func (d *DummyMailServer) Outbox(apiKey string, address string) []*Message

Outbox returns the messages in the outbox for the given API key and address.

func (*DummyMailServer) Send

func (d *DummyMailServer) Send(apiKey string, message Message) error

Send a message via the dummy mail server.

type Mailer

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

Mailer is the mailer client.

func New

func New(mailerType Type, options ...Option) *Mailer

New creates a new mailer.

func (*Mailer) SendMail

func (m *Mailer) SendMail(ctx context.Context, message Message) error

SendMail sends a mail.

type Message

type Message struct {
	From          mail.Address
	To            mail.Address
	Subject       string
	BodyPlainText string
	BodyHTML      string
}

Message is a piece of mail.

type Option

type Option func(*Config)

Option can be used to configure the mailer.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey sets the API key for the mailer.

func WithConfig

func WithConfig(config Config) Option

WithConfig sets the configuration for the mailer.

This fully replaces the existing configuration.

func WithDomain

func WithDomain(domain string) Option

WithDomain sets the domain for the mailer.

func WithRetry

func WithRetry(retry bool, options ...backoff.RetryOption) Option

WithRetry sets the retry configuration for the mailer.

type Type

type Type string

Type is the type of mailer.

const (
	TypeMailgun  Type = "mailgun"
	TypeSendgrid Type = "sendgrid"
)

Jump to

Keyboard shortcuts

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