Need help with go-smtp?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

emersion
660 Stars 104 Forks MIT License 223 Commits 14 Opened issues

Description

:outbox_tray: An SMTP client & server library written in Go

Services available

!
?

Need anything else?

Contributors list

# 19,081
golang
webrtc
vulkan
wayland...
110 commits
# 37,376
Go
Shell
c-plus-...
spf
51 commits
# 144,093
nginx
smtp-se...
golang
PHP
14 commits
# 127,074
Linux
Sass
TeX
icmp
6 commits
# 110,935
k8s
golang
HTML
Zsh
2 commits
# 550,768
Go
smtp
1 commit
# 423,315
PHP
smtp
Shell
HTML
1 commit
# 304,619
smtp
Shell
golang
Kuberne...
1 commit
# 130,869
Xamarin
vscode
floss
Lua
1 commit
# 101,846
Go
smtp
Shell
microse...
1 commit
# 210,551
Shell
pagespe...
C++
C
1 commit
# 7,198
Raspber...
ldap
curl
Perl
1 commit
# 436,377
Kotlin
Shell
HTML
softwar...
1 commit
# 10,581
Go
golang
html-em...
nacl
1 commit
# 567,713
Go
smtp
1 commit
# 441,682
Go
smtp
Shell
1 commit
# 524,239
Go
smtp
1 commit
# 52,647
mocking...
Groovy
test-fr...
Neovim
1 commit
# 323,251
Go
Shell
osd
HTML
1 commit
# 938
Go
gRPC
2fa
oauth2
1 commit

go-smtp

godocs.io builds.sr.ht status codecov

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("", "[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.

Server

package main

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

"github.com/emersion/go-smtp"

)

// 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, opts smtp.MailOptions) error { log.Println("Mail from:", from) return nil }

func (s *Session) Rcpt(to string) error { log.Println("Rcpt to:", to) 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 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:
RCPT TO:
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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.