Skip to content

Commit

Permalink
Update README server example, add server example in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Mar 22, 2019
1 parent 1a1363a commit 5381b0b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 12 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,32 @@ import (
type Backend struct{}

// Login handles a login command with username and password.
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.User, error) {
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
if username != "username" || password != "password" {
return nil, errors.New("Invalid username or password")
}
return &User{}, nil
return &Session{}, nil
}

// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.User, error) {
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
return nil, smtp.ErrAuthRequired
}

// A User is returned after successful login.
type User struct{}
// A Session is returned after successful login.
type Session struct{}

// Send handles an email.
func (u *User) Send(from string, to []string, r io.Reader) error {
log.Println("Sending message:", from, to)
func (s *Session) Mail(from string) error {
log.Println("Mail from:", from)
return nil
}

func (s *Session) Rcpt(to string) error {
log.Println("Rcpt to:", from)
return nil
}

func (s *Session) Data(r io.Reader) error {
if b, err := ioutil.ReadAll(r); err != nil {
return err
} else {
Expand All @@ -95,8 +102,9 @@ func (u *User) Send(from string, to []string, r io.Reader) error {
return nil
}

// Logout handles logout.
func (u *User) Logout() error {
func (s *Session) Reset() {}

func (s *Session) Logout() error {
return nil
}

Expand Down
69 changes: 67 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
package smtp_test

import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"strings"

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)

func Example() {
func ExampleDial() {
// Connect to the remote SMTP server.
c, err := smtp.Dial("mail.example.com:25")
if err != nil {
Expand Down Expand Up @@ -57,7 +60,7 @@ var (
recipients = []string{"[email protected]"}
)

func ExamplePlainAuth() {
func ExampleSendMail_PlainAuth() {
// hostname is used by PlainAuth to validate the TLS certificate.
hostname := "mail.example.com"
auth := sasl.NewPlainClient("", "[email protected]", "password")
Expand All @@ -84,3 +87,65 @@ func ExampleSendMail() {
log.Fatal(err)
}
}

// The Backend implements SMTP server methods.
type Backend struct{}

// Login handles a login command with username and password.
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
if username != "username" || password != "password" {
return nil, errors.New("Invalid username or password")
}
return &Session{}, nil
}

// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
return nil, smtp.ErrAuthRequired
}

// A Session is returned after successful login.
type Session struct{}

func (s *Session) Mail(from string) error {
log.Println("Mail from:", from)
return nil
}

func (s *Session) Rcpt(to string) error {
log.Println("Rcpt to:", from)
return nil
}

func (s *Session) Data(r io.Reader) error {
if b, err := ioutil.ReadAll(r); err != nil {
return err
} else {
log.Println("Data:", string(b))
}
return nil
}

func (s *Session) Reset() {}

func (s *Session) Logout() error {
return nil
}

func ExampleNewServer() {
be := &Backend{}

s := smtp.NewServer(be)

s.Addr = ":1025"
s.Domain = "localhost"
s.MaxIdleSeconds = 300
s.MaxMessageBytes = 1024 * 1024
s.MaxRecipients = 50
s.AllowInsecureAuth = true

log.Println("Starting server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 5381b0b

Please sign in to comment.