# README
go-smtp
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{}
func (bkd *Backend) NewSession(_ smtp.ConnectionState, _ string) (smtp.Session, error) {
return &Session{}, nil
}
// A Session is returned after EHLO.
type Session struct{}
func (s *Session) AuthPlain(username, password string) error {
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
return nil
}
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:<[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
# Packages
Package backendutil provide utilities to implement SMTP backends.
No description provided by the author
# Functions
Dial returns a new Client connected to an SMTP server at addr.
DialTLS returns a new Client connected to an SMTP server via TLS at addr.
DialWithSocks5 returns a new Client connected to an SMTP server via socks5 proxy at addr.
NewClient returns a new Client using an existing connection and host as a server name to be used when authenticating.
NewClientLMTP returns a new LMTP Client (as defined in RFC 2033) using an existing connector and host as a server name to be used when authenticating.
New creates a new SMTP server.
SendMail connects to the server at addr, switches to TLS, authenticates with the optional SASL client, and then sends an email from address from, to addresses to, with message r.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Attach the full copy of the message to any DSN that indicates a failure.
Attach only header of the message to any DSN that indicates a failure.
# Variables
EnhancedCodeNotSet is a nil value of EnhancedCode field in SMTPError, used to indicate that backend failed to provide enhanced status code.
No description provided by the author
No description provided by the author
ErrDataReset is returned by Reader pased to Data function if client does not send another BDAT command and instead closes connection or issues RSET command.
No description provided by the author
No description provided by the author
NoEnhancedCode is used to indicate that enhanced error code should not be included in response.
# Structs
A Client represents a client connection to an SMTP server.
No description provided by the author
No description provided by the author
MailOptions contains custom arguments that were passed as an argument to the MAIL command.
No description provided by the author
A SMTP server.
SMTPError specifies the error code, enhanced error code (if any) and message returned by the server.
# Interfaces
A SMTP server backend.
LMTPSession is an add-on interface for Session.
No description provided by the author
No description provided by the author
Session is used by servers to respond to an SMTP client.
StatusCollector allows a backend to provide per-recipient status information.
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
A function that creates SASL servers.