mailtest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package mailtest is an in-process fake SMTP server for tests: it speaks enough ESMTP (EHLO, STARTTLS, AUTH PLAIN/LOGIN, MAIL, RCPT, DATA) for net/smtp and records every accepted message.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	From string
	To   []string
	// Data is the raw message (headers + body, CRLF).
	Data string
	// User and Pass are the AUTH credentials of the session ("" without AUTH).
	User, Pass string
	// TLS reports whether the session was encrypted.
	TLS bool
}

Message is one accepted message.

func (Message) Code

func (m Message) Code() string

Code returns the first 6-digit code in the message body ("" when none).

func (Message) Header

func (m Message) Header() mail.Header

Header parses the message headers (nil on error).

func (m Message) Link() string

Link returns the first http(s) URL of the plain-text part, with quoted-printable soft breaks removed ("" when none).

type Option

type Option func(*Server)

Option configures a Server.

func WithAuth

func WithAuth(user, pass string) Option

WithAuth requires AUTH with these credentials.

func WithImplicitTLS

func WithImplicitTLS() Option

WithImplicitTLS serves SMTPS (TLS from the first byte).

func WithSTARTTLS

func WithSTARTTLS() Option

WithSTARTTLS offers STARTTLS with a self-signed certificate.

type Server

type Server struct {
	// Addr is host:port of the listener.
	Addr string
	// Host and Port split Addr.
	Host string
	Port int
	// contains filtered or unexported fields
}

Server is a fake SMTP server.

func Start

func Start(t testing.TB, opts ...Option) *Server

Start runs a server until the test ends.

func (*Server) Config

func (s *Server) Config() config.SMTPConfig

Config returns an SMTP configuration pointing at the server.

func (*Server) Last

func (s *Server) Last(t testing.TB) Message

Last waits for at least one message and returns the newest.

func (*Server) Messages

func (s *Server) Messages() []Message

Messages returns the accepted messages so far.

func (*Server) RejectRecipients

func (s *Server) RejectRecipients(v bool)

RejectRecipients makes RCPT TO fail with 550.

func (*Server) Wait

func (s *Server) Wait(t testing.TB, n int) []Message

Wait blocks until at least n messages arrived (fails the test after 5 s).

Jump to

Keyboard shortcuts

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