📤 An SMTP client & server library written in Go
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
emersion a631046577
Merge pull request #7 from bradleyshawkins/call-user
Calling User function instead of comparing User function to nil
2018-02-04 12:01:56 +01:00
backendutil Adds backendutil package 2017-02-20 21:29:04 +01:00
.gitignore Update .gitignore 2016-05-18 13:55:47 +02:00
.travis.yml Adds code coverage 2016-11-02 18:46:58 +01:00
backend.go Kills Message 2016-11-02 13:43:00 +01:00
client.go Make SendMail accept an io.Reader instead of a []byte 2016-11-02 14:10:39 +01:00
client_test.go Make SendMail accept an io.Reader instead of a []byte 2016-11-02 14:10:39 +01:00
conn.go Calling User function instead of comparing User function to nil 2018-02-03 23:15:16 -07:00
data.go go fmt 2016-11-02 14:17:09 +01:00
example_test.go Fixes example 2016-11-02 14:28:58 +01:00
LICENSE Adds go authors to LICENSE 2016-11-02 11:59:59 +01:00
parse.go Added variable to the error creation 2018-02-03 23:13:40 -07:00
README.md Adds client example to README 2016-11-02 19:16:34 +01:00
server.go Add Server.ForEachConn 2017-08-22 14:44:31 +02:00
server_test.go Adds test for too many unrecognized commands 2017-01-04 17:47:34 +01:00
smtp.go Updates packagfe description 2016-11-02 12:00:18 +01:00

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

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
}

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