testmail

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package testmail provides in-memory IMAP and SMTP servers for tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IMAP

type IMAP struct {
	Addr string
	User *imapmemserver.User
	// contains filtered or unexported fields
}

IMAP is a running in-memory IMAP server with one user, alice/secret.

func StartIMAP

func StartIMAP(t *testing.T, opts ...Option) *IMAP

StartIMAP starts a server on 127.0.0.1:0 with INBOX, Sent, Trash and Archive.

func (*IMAP) BlockLogin

func (s *IMAP) BlockLogin() (entered <-chan struct{}, release func())

BlockLogin is BlockLoginAfter(0).

func (*IMAP) BlockLoginAfter

func (s *IMAP) BlockLoginAfter(n int) (entered <-chan struct{}, release func())

BlockLoginAfter lets n logins (LOGIN or XOAUTH2) through and holds the next one until release is called (release may be called more than once); the held login checks its password or token only then. entered fires once it is held.

func (*IMAP) BytesSent

func (s *IMAP) BytesSent() int64

BytesSent is the total written to all client connections so far.

func (*IMAP) DropAfterNextAppend

func (s *IMAP) DropAfterNextAppend()

DropAfterNextAppend makes the next APPEND store the message and then close the client's connection before the tagged OK.

func (*IMAP) DropFetchAfter

func (s *IMAP) DropFetchAfter(n int)

DropFetchAfter lets n FETCH commands complete and drops the connection during the one after, like DropNextFetch.

func (*IMAP) DropNextFetch

func (s *IMAP) DropNextFetch()

DropNextFetch makes the next FETCH write at most one write of response and then close the client's connection.

func (*IMAP) DropOnNextUIDExpunge

func (s *IMAP) DropOnNextUIDExpunge()

DropOnNextUIDExpunge makes the next UID EXPUNGE close the client's connection without expunging anything.

func (*IMAP) IDs

func (s *IMAP) IDs() []map[string]string

IDs returns the parameters of every ID command received, in order.

func (*IMAP) Logins

func (s *IMAP) Logins() int64

Logins is how many LOGIN and AUTHENTICATE commands the server has handled.

func (*IMAP) MovesAndCopies

func (s *IMAP) MovesAndCopies() int64

MovesAndCopies is how many MOVE and COPY commands the server has handled.

func (*IMAP) Restart

func (s *IMAP) Restart()

Restart listens again on the same address with the same data.

func (*IMAP) SetPassword

func (s *IMAP) SetPassword(pw string)

SetPassword changes alice's password, as a user does on the provider's website.

func (*IMAP) Stop

func (s *IMAP) Stop()

Stop closes the listener and every live connection. Data is kept.

type Inbox

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

Inbox records what the test SMTP server received.

func StartSMTP

func StartSMTP(t *testing.T, opts ...SMTPOption) (addr string, in *Inbox)

StartSMTP starts a plaintext SMTP server accepting only alice/secret (PLAIN).

func (*Inbox) AuthAttempts

func (in *Inbox) AuthAttempts() int

AuthAttempts is how many AUTH exchanges the server has handled.

func (*Inbox) BlockData

func (in *Inbox) BlockData() (entered <-chan struct{}, release func())

BlockData makes the next sessions hold their reply after reading the full DATA until release is called (release may be called more than once). entered receives once per held session.

func (*Inbox) FailAfterData

func (in *Inbox) FailAfterData(n int)

FailAfterData makes the next n sessions read the full DATA and then drop the connection without replying. The server has the message, so it is recorded.

func (*Inbox) Messages

func (in *Inbox) Messages() []Received

Messages returns a copy of the received messages.

func (*Inbox) RejectAuth

func (in *Inbox) RejectAuth(reject bool)

RejectAuth makes the server refuse (or, with false, accept again) alice/secret.

func (*Inbox) SetPassword

func (in *Inbox) SetPassword(pw string)

SetPassword changes alice's password.

type Option

type Option func(*config)

Option customises StartIMAP and StartSMTP; each option says which servers it affects.

func RequireID

func RequireID() Option

RequireID makes the IMAP server refuse SELECT with "NO Unsafe Login" until the client has sent ID, as NetEase does.

func WithXOAuth2

func WithXOAuth2(token string) Option

WithXOAuth2 makes the IMAP and SMTP servers offer XOAUTH2 and accept token for alice.

func WithXOAuth2Func

func WithXOAuth2Func(valid func(token string) bool) Option

WithXOAuth2Func is WithXOAuth2 with a validator, such as a testoauth server's ValidAccess. The XOAUTH2 user may be alice or an alice@ address.

func WithoutCOPYUID

func WithoutCOPYUID() Option

WithoutCOPYUID keeps UIDPLUS advertised but suppresses COPYUID response codes.

func WithoutIdle

func WithoutIdle() Option

WithoutIdle removes IDLE from the advertised capabilities (the server adds it on its own, so the connection strips it from CAPABILITY lines).

func WithoutMove

func WithoutMove() Option

WithoutMove removes MOVE from the advertised capabilities.

func WithoutUIDPlus

func WithoutUIDPlus() Option

WithoutUIDPlus removes UIDPLUS and suppresses COPYUID/APPENDUID response codes (the memory backend always returns them).

type Received

type Received struct {
	From string
	To   []string
	Data []byte
}

Received is one message accepted by the test SMTP server.

type SMTPOption

type SMTPOption = Option

SMTPOption customises StartSMTP; WithXOAuth2 applies to both servers.

func SaveToSent

func SaveToSent(im *IMAP) SMTPOption

SaveToSent makes the SMTP server file every accepted message into im's Sent folder, as providers that auto-save sent mail do.

Jump to

Keyboard shortcuts

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