smtptest

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 13 Imported by: 0

README

smtptest build Coverage Code to Test Ratio Test Execution Time

smtptest provides SMTP server for testing.

Usage

package main

import (
	"fmt"
	"net/smtp"
	"strings"
	"testing"

	"github.com/k1LoW/smtptest"
)

const testMsg = "To: alice@example.net\r\n" +
	"Subject: Hello Gophers!\r\n" +
	"\r\n" +
	"This is the email body.\r\n"

func TestSendMail(t *testing.T) {
	ts, auth, err := smtptest.NewServerWithAuth()
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		ts.Close()
	})

	addr := ts.Addr()
	if err := smtp.SendMail(addr, auth, "sender@example.org", []string{"alice@example.net"}, []byte(testMsg)); err != nil {
		t.Fatal(err)
	}

	if len(ts.Messages()) != 1 {
		t.Errorf("got %v\nwant %v", len(ts.Messages()), 1)
	}
	msgs := ts.Messages()

	got := msgs[0].Header.Get("To")
	want := "alice@example.net"
	if got != want {
		t.Errorf("got %v\nwant %v", got, want)
	}
}

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v0.9.0

type Option func(*backend) error

func WithOnReceiveFunc added in v0.9.0

func WithOnReceiveFunc(fn onReceiveFunc) Option

func WithPlainAuth added in v0.9.0

func WithPlainAuth(username, password string) Option

type Server

type Server struct {
	Host string
	Port int
	Err  error
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ...Option) (*Server, error)

func NewServerWithAuth added in v0.3.0

func NewServerWithAuth() (*Server, netsmtp.Auth, error)

func (*Server) Addr added in v0.2.0

func (s *Server) Addr() string

func (*Server) Close

func (s *Server) Close()

func (*Server) Messages

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

func (*Server) OnReceive added in v0.8.0

func (s *Server) OnReceive(fn func(from, to string, recipients []string, msg *mail.Message) error)

Deprecated

func (*Server) RawMessages added in v0.5.0

func (s *Server) RawMessages() []io.Reader

func (*Server) Sessions

func (s *Server) Sessions() []*Session

type Session

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

func (*Session) Auth added in v0.9.0

func (s *Session) Auth(mech string) (sasl.Server, error)

func (*Session) AuthMechanisms added in v0.9.0

func (s *Session) AuthMechanisms() []string

func (*Session) Data

func (s *Session) Data(r io.Reader) error

func (*Session) From

func (s *Session) From() string

func (*Session) Logout

func (s *Session) Logout() error

func (*Session) Mail

func (s *Session) Mail(from string, opts *smtp.MailOptions) error

func (*Session) Message

func (s *Session) Message() *mail.Message

func (*Session) RawMessage added in v0.5.0

func (s *Session) RawMessage() io.Reader

func (*Session) Rcpt

func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error

func (*Session) Recipients added in v0.7.0

func (s *Session) Recipients() []string

func (*Session) Reset

func (s *Session) Reset()

func (*Session) To deprecated

func (s *Session) To() string

Deprecated: use Recipients instead to retrieve all recipients.

Jump to

Keyboard shortcuts

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