testmail

package
v1.34.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 33 Imported by: 0 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.

Documentation

Overview

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

Index

Constants

View Source
const (
	GmailAllMail = "[Gmail]/All Mail"
	GmailSent    = "[Gmail]/Sent Mail"
	GmailDrafts  = "[Gmail]/Drafts"
	GmailTrash   = "[Gmail]/Trash"
	GmailSpam    = "[Gmail]/Spam"
)

Gmail's folders, which a Gmail server starts with besides INBOX.

Variables

This section is empty.

Functions

This section is empty.

Types

type DKIMFixture added in v1.21.0

type DKIMFixture struct {
	Records map[string][]string
	// contains filtered or unexported fields
}

DKIMFixture signs real message bytes and serves only deterministic public DNS test records. No real mailbox, key or external DNS service is used.

func NewDKIMFixture added in v1.21.0

func NewDKIMFixture(t testing.TB) *DKIMFixture

func (*DKIMFixture) LookupTXT added in v1.21.0

func (f *DKIMFixture) LookupTXT(ctx context.Context, name string) ([]string, error)

func (*DKIMFixture) Sign added in v1.21.0

func (f *DKIMFixture) Sign(t testing.TB, from, domain string, options ...func(*dkim.SignOptions)) []byte

type GmailMessage added in v1.30.0

type GmailMessage struct {
	ID uint64 // X-GM-MSGID
	// contains filtered or unexported fields
}

GmailMessage is one Gmail message, in the folders it has a UID in.

func (*GmailMessage) Folders added in v1.30.0

func (m *GmailMessage) Folders() []string

Folders lists the folders the message is in, sorted.

func (*GmailMessage) Label added in v1.30.0

func (m *GmailMessage) Label(folder string)

Label adds the message to folder, as another client does: a new UID there, with the message's flags.

func (*GmailMessage) UID added in v1.30.0

func (m *GmailMessage) UID(folder string) imap.UID

UID is the message's UID in folder, 0 when it is not there.

func (*GmailMessage) Unlabel added in v1.30.0

func (m *GmailMessage) Unlabel(folder string)

Unlabel takes the message out of folder, as archiving or removing a label does in another client.

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 (with Gmail, INBOX and the Gmail folders).

func (*IMAP) AddGmail added in v1.30.0

func (s *IMAP) AddGmail(raw string, flags []imap.Flag, folders ...string) *GmailMessage

AddGmail adds a new Gmail message with flags to folders, which must exist.

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) DropAfterGmailLabelStore added in v1.30.0

func (s *IMAP) DropAfterGmailLabelStore(n int)

DropAfterGmailLabelStore drops the response after the nth next label STORE.

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) GmailLabelStores added in v1.30.0

func (s *IMAP) GmailLabelStores() int64

GmailLabelStores is the number of label STORE commands handled.

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) RejectNextGmailSearch added in v1.30.0

func (s *IMAP) RejectNextGmailSearch()

RejectNextGmailSearch makes one identity check return NO without a result.

func (*IMAP) RejectNextGmailSelect added in v1.30.0

func (s *IMAP) RejectNextGmailSelect()

RejectNextGmailSelect makes one membership check return NO for an existing folder.

func (*IMAP) ResetUIDValidity added in v1.33.0

func (s *IMAP) ResetUIDValidity(folder string)

ResetUIDValidity makes SELECT report a new UIDVALIDITY for folder from now on, as a server that recreated it would; the messages and UIDs stay.

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) DisableAuth added in v1.32.0

func (in *Inbox) DisableAuth(off bool)

DisableAuth makes AUTH answer what Microsoft does for a mailbox whose SMTP AUTH is disabled (535 5.7.139), whatever the credentials.

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) Reply added in v1.14.0

func (in *Inbox) Reply(to string, at SMTPStage, resp *smtp.SMTPError, once bool)

Reply makes the server answer resp for the recipient to at stage: at its RCPT TO, or once the full DATA of a message to it is read (the message is then not recorded). With once it answers only the next time, then accepts again; otherwise every time until Reply is called again. A nil resp clears it.

func (*Inbox) SetPassword

func (in *Inbox) SetPassword(pw string)

SetPassword changes alice's password.

type JMAP added in v1.33.0

type JMAP struct {
	*jmapmemserver.Server
	Inbox gojmap.ID
	// contains filtered or unexported fields
}

JMAP is a running in-memory JMAP server with one account, alice/secret, that starts with an inbox. Its hooks (ExpireState, DropEventSources, MergeEmails, RejectNextRequest, Redirect, SeparateDownloadHost) inject faults.

func StartJMAP added in v1.33.0

func StartJMAP(t testing.TB) *JMAP

StartJMAP starts a JMAP server, closed when t ends.

func (*JMAP) Deliver added in v1.33.0

func (s *JMAP) Deliver(folder string, raw []byte) gojmap.ID

Deliver puts raw into the mailbox named folder, as mail arriving.

func (*JMAP) Mailbox added in v1.33.0

func (s *JMAP) Mailbox(name string) gojmap.ID

Mailbox is the id of the mailbox named name, created if need be; "INBOX" is the inbox.

func (*JMAP) Password added in v1.33.0

func (s *JMAP) Password() string

Password is the password a client should use: "secret", or a wrong one after RejectAuth.

func (*JMAP) RejectAuth added in v1.33.0

func (s *JMAP) RejectAuth()

RejectAuth makes Password wrong from now on, so the server refuses it.

func (*JMAP) SessionURL added in v1.33.0

func (s *JMAP) SessionURL() string

SessionURL is where the session resource is.

type Option

type Option func(*config)

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

func Gmail added in v1.30.0

func Gmail() Option

Gmail makes the IMAP server behave as Gmail: it advertises X-GM-EXT-1, starts with INBOX and the Gmail folders (special-use in LIST), and keeps messages added with AddGmail as Gmail does. One such message is in several folders, each with its own UID; flags other than \Deleted are shared; FETCH answers X-GM-MSGID and X-GM-LABELS; COPY and MOVE add the destination to its folders (with the UID it already has there, if any), and MOVE and EXPUNGE take it out of the source; a MOVE to Trash or Spam takes it out of every other folder. Identity is never derived from content: two AddGmail calls make two messages, and an APPEND makes a new one.

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 POP3 added in v1.34.0

type POP3 struct {
	Addr  string
	Roots *x509.CertPool // trusts the certificate STLS presents
	// contains filtered or unexported fields
}

POP3 is a running in-memory POP3 server with one maildrop, alice/secret. It speaks USER/PASS, STLS, STAT, LIST, UIDL, RETR, TOP, DELE, RSET, NOOP and QUIT; deletions take effect at QUIT, as RFC 1939 says.

func StartPOP3 added in v1.34.0

func StartPOP3(t testing.TB) *POP3

StartPOP3 starts a server on 127.0.0.1:0, closed when t ends.

func (*POP3) Deletes added in v1.34.0

func (s *POP3) Deletes() int

Deletes is how many DELE commands took effect at a QUIT.

func (*POP3) Deliver added in v1.34.0

func (s *POP3) Deliver(raw []byte) string

Deliver appends raw to the maildrop and returns its UIDL.

func (*POP3) DisableTOP added in v1.34.0

func (s *POP3) DisableTOP()

DisableTOP makes the server refuse TOP, which RFC 1939 leaves optional.

func (*POP3) DisableUIDL added in v1.34.0

func (s *POP3) DisableUIDL()

DisableUIDL makes the server answer UIDL with -ERR, as one without it does.

func (*POP3) FailRETR added in v1.34.0

func (s *POP3) FailRETR(n int)

FailRETR makes every RETR after the next n fail; n < 0 lets them all succeed again.

func (*POP3) Port added in v1.34.0

func (s *POP3) Port() int

Port is the port the server listens on.

func (*POP3) RejectAuth added in v1.34.0

func (s *POP3) RejectAuth()

RejectAuth makes the server refuse alice's password from now on.

func (*POP3) RejectDele added in v1.34.0

func (s *POP3) RejectDele()

RejectDele makes the server refuse DELE from now on.

func (*POP3) Remove added in v1.34.0

func (s *POP3) Remove(uid string)

Remove deletes the message uid from the maildrop, as another client would.

func (*POP3) STLS added in v1.34.0

func (s *POP3) STLS() int

STLS is how many connections the server has upgraded with STLS.

func (*POP3) UIDs added in v1.34.0

func (s *POP3) UIDs() []string

UIDs is the maildrop's UIDLs, oldest first.

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 LoginOnly added in v1.24.1

func LoginOnly() SMTPOption

LoginOnly makes the SMTP server offer AUTH LOGIN and not PLAIN, as Exchange does.

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.

type SMTPStage added in v1.14.0

type SMTPStage int

SMTPStage is when a Reply answers.

const (
	AtRcpt SMTPStage = iota // RCPT TO
	AtData                  // the end of DATA
)

Jump to

Keyboard shortcuts

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