Skip to content

Commit

Permalink
Merge pull request #8 from gomicro/docs-and-readme
Browse files Browse the repository at this point in the history
Docs and readme
  • Loading branch information
dan9186 authored Oct 21, 2017
2 parents b4d7349 + 8a1e1c0 commit d155daf
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 59 deletions.
75 changes: 16 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
# Postal
[![Build Status](https://travis-ci.org/gomicro/postal.svg)](https://travis-ci.org/gomicro/postal)
[![Coverage](http://gocover.io/_badge/github.com/gomicro/postal)](http://gocover.io/github.com/gomicro/postal)
[![GoDoc](https://godoc.org/github.com/gomicro/postal?status.png)](https://godoc.org/github.com/gomicro/postal)
[![Go Reportcard](https://goreportcard.com/badge/github.com/gomicro/postal)](https://goreportcard.com/report/github.com/gomicro/postal)
[![GoDoc](https://godoc.org/github.com/gomicro/postal?status.svg)](https://godoc.org/github.com/gomicro/postal)
[![License](https://img.shields.io/github/license/gomicro/postal.svg)](https://github.com/gomicro/postal/blob/master/LICENSE.md)
[![Release](https://img.shields.io/github/release/gomicro/postal.svg)](https://github.com/gomicro/postal/releases/latest)

Postal is a mocked SMTP mailer.

# Usage
Compare a mail record

```
func testMailing(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))
}
# Requirements
Golang version 1.6 or higher

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

if r.From != from {
t.Errorf("expected %v, got %v", from, r.From)
}
if r.To != to {
t.Errorf("expected %v, got %v", to, r.To)
}
if r.Body != body {
t.Errorf("expected %v, got %v", string(body), string(r.Body))
}
}
```

Get an error from sending an email

go get github.com/gomicro/postal
```
func testMailing(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()
# Usage
See the [examples](https://godoc.org/github.com/gomicro/postal#pkg-examples) within the docs for ways to use the library.

err := SendMail("mailhost.com:25", auth, "dev@gomicro.io", []string{"foo@bar.com"}, []byte("Hello world"))
# Versioning
The library will be versioned in accordance with [Semver 2.0.0](http://semver.org). See the [releases](https://github.com/gomicro/postal/releases) section for the latest version. Until version 1.0.0 the libary is considered to be unstable.

if err == nil {
t.Errorf("Expected error, and got nil")
}
}
```
It is always highly recommended to vendor the version you are using.

# License
See [LICENSE.md](./LICENSE.md) for more information.
1 change: 1 addition & 0 deletions postal.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//Package postal is a mock SMTP mailer
package postal

import (
Expand Down
67 changes: 67 additions & 0 deletions postal_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package postal_test

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

"github.com/gomicro/postal"
)

func ExamplePostal() {
// 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))
}
}

func ExamplePostal_error() {
// 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")
}
}

0 comments on commit d155daf

Please sign in to comment.