• Stars
    star
    1,701
  • Rank 27,447 (Top 0.6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ“ค An SMTP client & server library written in Go

go-smtp

godocs.io builds.sr.ht status

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() {
	// Setup authentication information.
	auth := sasl.NewPlainClient("", "[email protected]", "password")

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

If you need more control, you can use Client instead. For example, if you want to send an email via a server without TLS or auth support, you can do something like this:

package main

import (
	"log"
	"strings"

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

func main() {
	// Setup an unencrypted connection to a local mail server.
	c, err := smtp.Dial("localhost:25")
	if err != nil {
		return err
	}
	defer c.Close()

	// Set the sender and recipient, and send the email all in one step.
	to := []string{"[email protected]"}
	msg := strings.NewReader("To: [email protected]\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n")
	err := c.SendMail("[email protected]", to, msg)
	if err != nil {
		log.Fatal(err)
	}
}

Server

package main

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

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

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

func (bkd *Backend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
	return &Session{}, nil
}

// A Session is returned after EHLO.
type Session struct {
	auth bool
}

func (s *Session) AuthPlain(username, password string) error {
	if username != "username" || password != "password" {
		return smtp.ErrAuthFailed
	}
	s.auth = true
	return nil
}

func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
	if !s.auth {
		return smtp.ErrAuthRequired
	}
	log.Println("Mail from:", from)
	return nil
}

func (s *Session) Rcpt(to string) error {
	if !s.auth {
		return smtp.ErrAuthRequired
	}
	log.Println("Rcpt to:", to)
	return nil
}

func (s *Session) Data(r io.Reader) error {
	if !s.auth {
		return smtp.ErrAuthRequired
	}
	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 main() {
	be := &Backend{}

	s := smtp.NewServer(be)

	s.Addr = ":1025"
	s.Domain = "localhost"
	s.ReadTimeout = 10 * time.Second
	s.WriteTimeout = 10 * time.Second
	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:<[email protected]>
RCPT TO:<[email protected]>
DATA
Hey <3
.

Relationship with net/smtp

The Go standard library provides a SMTP client implementation in net/smtp. However net/smtp is frozen: it's not getting any new features. go-smtp provides a server implementation and a number of client improvements.

Licence

MIT

More Repositories

1

mako

A lightweight Wayland notification daemon
C
2,162
star
2

go-imap

๐Ÿ“ฅ An IMAP library for clients and servers
Go
2,064
star
3

hydroxide

A third-party, open-source ProtonMail CardDAV, IMAP and SMTP bridge
Go
1,190
star
4

slurp

Select a region in a Wayland compositor
C
932
star
5

grim

Grab images from a Wayland compositor
C
831
star
6

kanshi

Dynamic display configuration (mirror)
C
608
star
7

xdg-desktop-portal-wlr

xdg-desktop-portal backend for wlroots
C
591
star
8

neutron

Self-hosted server for the ProtonMail client
Go
513
star
9

mrsh

A minimal POSIX shell
C
478
star
10

go-message

โœ‰๏ธ A streaming Go library for the Internet Message Format and mail messages
Go
379
star
11

go-webdav

A Go library for WebDAV, CalDAV and CardDAV
Go
314
star
12

go-autostart

A Go library to run a command after login
Go
139
star
13

go-msgauth

A Go library and tools for DKIM, DMARC and Authentication-Results
Go
129
star
14

soju

IRC bouncer (mirror)
Go
123
star
15

hello-wayland

A hello world Wayland client, 2018 edition
C
106
star
16

bups

Simple GUI for Bup, a very efficient backup system.
Python
96
star
17

wlr-randr

An xrandr clone for wlroots compositors
C
94
star
18

go-sasl

๐Ÿ”‘ A SASL library written in Go
Go
89
star
19

go-vcard

A Go library to parse and format vCard
Go
88
star
20

libliftoff

Lightweight KMS plane library
C
75
star
21

go-mbox

Package mbox parses the mbox file format into messages and formats messages into mbox files
Go
68
star
22

basu

The sd-bus library, extracted from systemd
C
60
star
23

emuarius

Bridge between Twitter and Mastodon
Go
58
star
24

stability-badges

SVG badges for Go projects
55
star
25

go-pgpmail

๐Ÿ”’ PGP-encrypted email library for Go
Go
50
star
26

python-emailthreads

Python library to parse and format email threads
Python
42
star
27

go-milter

Go library to write mail filters
Go
41
star
28

go-ical

An iCalendar library for Go
Go
40
star
29

go-dkim

A Go library to create and verify DKIM signatures (migrated)
Go
38
star
30

wleird

A collection a Wayland clients doing weird things, for compositor testing
C
37
star
31

go-ostatus

An OStatus library written in Go
Go
32
star
32

go-maildir

A maildir library for Go
Go
32
star
33

go-imap-idle

IDLE extension for go-imap
Go
32
star
34

matcha

A read-only web interface for Git repositories
Go
29
star
35

go-smtp-proxy

A go-smtp-server backend that proxies messages to another SMTP server
Go
26
star
36

meltsub

Convert hardsub to softsub
Python
22
star
37

wlhangul

A Hangul input method for Wayland
C
22
star
38

go-imap-proxy

A go-imap backend that proxies all commands and responses to another IMAP server
Go
21
star
39

net-browserify

net module for browserify, with a websocket server proxy
JavaScript
18
star
40

klaes

An OpenPGP keyserver
Go
16
star
41

drm_monitor

A CLI tool to monitor DRM/KMS state
C
16
star
42

passwordmaker

PasswordMaker powered by WebExtension
JavaScript
15
star
43

glider

Experimental libliftoff-based Wayland compositor
C
15
star
44

phproxy

A proxy in PHP
PHP
13
star
45

go-imap-sortthread

SORT and THREAD extensions for go-imap
Go
13
star
46

ganbo

A WebExtension to export cookies into a cookies.txt file
JavaScript
12
star
47

node-i2c-mpu6050

Read data from MPU6050 with i2c
JavaScript
12
star
48

go-outlook-autodiscover

Generate Microsoft Outlook autodiscover files
Go
12
star
49

php-wrappers

High-performance wrappers for native FTP and SFTP functions.
PHP
12
star
50

firefox-passwordmaker

[UNMAINTAINED] PasswordMaker extension for Firefox
JavaScript
11
star
51

go-openpgp-hkp

A Go library for OpenPGP HTTP Keyserver Protocol (HKP)
Go
10
star
52

go-kdeconnect

A Go library for KDEConnect
Go
10
star
53

what-does-a-linker-do

A collection of small examples demonstrating various linker features
Makefile
10
star
54

go-smtp-mta

A Mail Transfer Agent backend for go-smtp
Go
10
star
55

go-apple-mobileconfig

Create Apple mobileconfig configuration files
Go
10
star
56

osu-skin-default

Default osu! skin
10
star
57

voice-recognition.js

A Javascript library for low-level voice recognition.
JavaScript
10
star
58

go-upnp-igd

Minimal Go UPnP InternetGatewayDevice library
Go
10
star
59

tooru

Archive and sync content you want to see twice
Shell
9
star
60

webpass

A web interface for pass, a UNIX password manager
Go
9
star
61

go-imap-uidplus

UIDPLUS extension for go-imap
Go
9
star
62

nanum-gothic-coding

Nanum Gothic Coding font
CSS
9
star
63

go-imap-id

The IMAP ID Extension for go-imap
Go
9
star
64

go-appdir

Go package to get application directories such as config and cache
Go
8
star
65

facebook-irc-bridge

Bridge between IRC channels and Facebook chat
JavaScript
8
star
66

go-tsm

Terminal-emulator State Machine library for Go
Go
8
star
67

go-mpris

A Go library for MPRIS
Go
8
star
68

go-prefixspan

A Go implementation of the PrefixSpan algorithm
Go
8
star
69

go-textwrapper

A writer that wraps long text lines to a specified length
Go
7
star
70

go-imap-multi

A go-imap backend relying on multiple other backends
Go
7
star
71

go-imap-quota

QUOTA extension for go-imap
Go
7
star
72

go-mtasts

A Go library for SMTP MTA Strict Transport Security
Go
7
star
73

tls-browserify

TLS module for Browserify
JavaScript
6
star
74

go-imap-mbox

A go-imap backend that stores mails in a MBOX file
Go
6
star
75

go-imap-appendlimit

The IMAP APPENDLIMIT Extension for go-imap
Go
6
star
76

caddy-wkd

OpenPGP Web Key Directory plugin for Caddy
Go
6
star
77

node-client-vm

Node client virtual machine - Node.js in the browser!
JavaScript
6
star
78

rust-osu-format

A Rust library to parse .osu files
Rust
6
star
79

go-tryalgo

Basic and (maybe) advanced algorithms and data structures in Go
Go
6
star
80

unfacebookify

Allow unregistered Facebook users to access a group's posts
JavaScript
6
star
81

go-openpgp-wkd

A Go library for OpenPGP Web Key Directory
Go
6
star
82

go-imap-specialuse

Special-Use Mailboxes extension for go-imap
Go
5
star
83

go-imap-metadata

IMAP METADATA extension for go-imap
Go
5
star
84

chromiumos-platform2

[fork] CrOS platform Wayland hacks
C++
5
star
85

go-jsonld

A Go library for JSON-LD
Go
5
star
86

gnomeconnect

A GNOME frontend for KDEConnect
Go
5
star
87

rust-edid

EDID parser in Rust
Rust
5
star
88

minilustre

A tiny compiler for a subset of Lustre
Go
5
star
89

node-servoblaster

ServoBlaster library for Node.js
JavaScript
5
star
90

node-mpu6050-server

A simple test server to visualize data from MPU6050
Python
4
star
91

rustre

A Lustre transpiler to Rust
Rust
4
star
92

node-magi-network

An experimental Magi supercomputer network
JavaScript
4
star
93

go-imap-enable

The IMAP ENABLE Extension for go-imap
Go
4
star
94

remark-server

A CLI tool to serve remark presentations
JavaScript
4
star
95

go-imap-disk

A go-imap backend that stores mails to disk
Go
4
star
96

go-oauthdialog

A Go library to present an OAuth dialog to the user
Go
4
star
97

go-js-canvas

GopherJS bindings for <canvas>
Go
4
star
98

do-dyn-domain

Digital Ocean dynamic domain updater
Go
4
star
99

emersion.fr

Personal Website
HTML
4
star
100

node-unparse

An open-source clone of Parse
JavaScript
4
star