emailmock

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package emailmock is a dev-only implementation of email.Sender that ships messages to the hamr dev server's mail inbox at /__hamr/mail.

The mock does not construct MIME, talk SMTP, or call any provider API. It serializes the email.Message to JSON and POSTs it to the hamr dev proxy, which stores it in an in-memory inbox viewable at http://<dev-proxy>/__hamr/mail.

Because the wire format is JSON (not MIME), this mock validates how your application uses the email.Sender interface — it does not validate your production provider adapter's MIME construction or SMTP handshake. Catch those bugs with your provider's test environment or integration tests.

Typical wiring in main.go:

var sender email.Sender
if os.Getenv("EMAIL_MOCK") == "true" {
    sender = emailmock.New(os.Getenv("HAMR_DEV_URL"))
} else {
    sender = myapp.NewRealSender(...)
}

The mock client is safe for concurrent use.

Failure simulation: any recipient whose local-part is "bounce" (e.g. bounce@example.com) causes Send to return ErrBounced without storing the message; any recipient whose local-part is "reject" returns ErrRejected. This lets apps test error paths without configuring anything server-side.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBounced  = errors.New("emailmock: recipient bounced")
	ErrRejected = errors.New("emailmock: recipient rejected")
)

Well-known failure outcomes returned by Send when the message hits a magic recipient. These wrap the server-side refusal so apps can test bounce and reject error paths deterministically.

Functions

This section is empty.

Types

type Client

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

Client is a dev-only email.Sender that POSTs messages to the hamr dev server's /__hamr/mail/ingest endpoint.

func New

func New(baseURL string) *Client

New returns a Client that ingests messages at baseURL/__hamr/mail/ingest. baseURL is typically the hamr dev proxy origin (e.g. "http://localhost:3000"). Trailing slashes are stripped. An empty baseURL is accepted but every Send will fail until the client is re-configured.

func (*Client) Send

func (c *Client) Send(ctx context.Context, msg email.Message) (*email.Result, error)

Send JSON-encodes msg and POSTs it to the mock inbox. It returns the server-assigned Result on success, ErrBounced/ErrRejected for magic recipients, or a wrapped transport/HTTP error otherwise.

Jump to

Keyboard shortcuts

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