📤 An SMTP client & server library written in Go
Clone or download
emersion Remove Conn.IsTLS
Use Conn.TLSConnectionState instead.

Fixes #20
Latest commit 9dc4d6f Jan 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
backendutil backendutil: allow Transform to return an error, support AnonymousLogin Jan 11, 2019
.gitignore Update .gitignore May 18, 2016
.travis.yml Adds code coverage Nov 2, 2016
LICENSE Adds go authors to LICENSE Nov 2, 2016
README.md Make it clear LMTP is supported Jan 9, 2019
backend.go
client.go client: return error from SendMail when required AUTH not available Jan 16, 2019
client_test.go Update client test keys Jan 16, 2019
conn.go Remove Conn.IsTLS Jan 16, 2019
data.go go fmt Nov 2, 2016
example_test.go Fixes example Nov 2, 2016
go.mod Add go.mod Sep 17, 2018
parse.go Merge branch 'master' into case-insensitive Sep 17, 2018
server.go Remove Server.ListenAndServeUnix Jan 9, 2019
server_test.go
smtp.go

README.md

go-smtp

GoDoc Build Status codecov stability-unstable Go Report Card

An ESMTP client and server library written in Go.

Features

  • ESMTP client & server implementing RFC 5321
  • Support for SMTP AUTH and PIPELINING
  • UTF-8 support for subject and message
  • LMTP support

Usage

Client

package main

import (
	"log"
	"strings"

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

func main() {
	// Set up authentication information.
	auth := sasl.NewPlainClient("", "user@example.com", "password")

	// Connect to the server, authenticate, set the sender and recipient,
	// and send the email all in one step.
	to := []string{"recipient@example.net"}
	msg := strings.NewReader("To: recipient@example.net\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n")
	err := smtp.SendMail("mail.example.com:25", auth, "sender@example.org", to, msg)
	if err != nil {
		log.Fatal(err)
	}
}

If you need more control, you can use Client instead.

Server

package main

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

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

type Backend struct{}

func (bkd *Backend) Login(username, password string) (smtp.User, error) {
	if username != "username" || password != "password" {
		return nil, errors.New("Invalid username or password")
	}
	return &User{}, nil
}

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

type User struct{}

func (u *User) Send(from string, to []string, r io.Reader) error {
	log.Println("Sending message:", from, to)

	if b, err := ioutil.ReadAll(r); err != nil {
		return err
	} else {
		log.Println("Data:", string(b))
	}
	return nil
}

func (u *User) Logout() error {
	return nil
}

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

You can use the server manually with telnet:

$ telnet localhost 1025
EHLO localhost
AUTH PLAIN
AHVzZXJuYW1lAHBhc3N3b3Jk
MAIL FROM:<root@nsa.gov>
RCPT TO:<root@gchq.gov.uk>
DATA
Hey <3
.

Licence

MIT