postal

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2017 License: MIT Imports: 1 Imported by: 0

README

Postal

Build Status Go Reportcard GoDoc License Release

Postal is a mocked SMTP mailer.

Requirements

Golang version 1.6 or higher

Installation

go get github.com/gomicro/postal

Usage

See the examples within the docs for ways to use the library.

Versioning

The library will be versioned in accordance with Semver 2.0.0. See the releases section for the latest version. Until version 1.0.0 the libary is considered to be unstable.

It is always highly recommended to vendor the version you are using.

License

See LICENSE.md for more information.

Documentation

Overview

Package postal is a mock SMTP mailer

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MailRecord

type MailRecord struct {
	Host string
	Auth smtp.Auth
	From string
	To   []string
	Body []byte
}

MailRecord represents a single record of the SMTP transaction sent

type Postal

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

Postal represents a mocked SMTP mailer

Example
package main

import (
	"net/smtp"
	"testing"

	"github.com/gomicro/postal"
)

func main() {
	// This would normally be provided by the testing function
	var t *testing.T

	host := "mailhost.com:25"
	auth := smtp.PlainAuth("", "username", "password", "mailhost.com")
	from := "dev@gomicro.io"
	to := []string{"foo@bar.com"}
	body := []byte("hello world")

	p := postal.New()
	SendMail := p.Mailer()

	err := SendMail(host, auth, from, to, body)
	if err != nil {
		t.Error(err.Error())
	}

	if p.Mailed() != 1 {
		t.Errorf("expected 1 mailed, got %v mailed", p.Mailed())
	}

	records := p.MailRecords()
	if len(records) != 1 {
		t.Errorf("expected 1 record, got %v record", len(records))
	}

	r := records[0]
	if r.Host != host {
		t.Errorf("expected %v, got %v", host, r.Host)
	}

	if r.From != from {
		t.Errorf("expected %v, got %v", from, r.From)
	}

	if string(r.Body) != string(body) {
		t.Errorf("expected %v, got %v", string(body), string(r.Body))
	}
}
Output:

Example (Error)
package main

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

	"github.com/gomicro/postal"
)

func main() {
	// This would normally be provided by the testing function
	var t *testing.T

	auth := smtp.PlainAuth("", "username", "password", "mailhost.com")

	p := postal.New()
	p.SetError(fmt.Errorf("something's not quite right here"))
	SendMail := p.Mailer()

	err := SendMail("mailhost.com:25", auth, "dev@gomicro.io", []string{"foo@bar.com"}, []byte("Hello world"))

	if err == nil {
		t.Errorf("Expected error, and got nil")
	}
}
Output:

func New

func New() *Postal

New initializes and returns a new Postal object

func (*Postal) MailRecords

func (p *Postal) MailRecords() []MailRecord

MailRecords returns the collection of mail records Postal has captured

func (*Postal) Mailed

func (p *Postal) Mailed() int

Mailed returns the number of messages mailed

func (*Postal) Mailer

func (p *Postal) Mailer() func(string, smtp.Auth, string, []string, []byte) error

Mailer returns a mocked smtp mailer method capture and force actions for assertions

func (*Postal) SetError

func (p *Postal) SetError(err error)

SetError sets the error the mailer is to return when called

Jump to

Keyboard shortcuts

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