package module
v0.0.0-...-fd3b5e4 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2016 License: MIT Imports: 13 Imported by: 0


Package smtpd is SMTP server library. go doc:

Forked from



Package smtpd is a SMTP server.




View Source
const (
	StatusPasswordNeeded       = 432
	StatusMessageError         = 450
	StatusMessageExceedStorage = 452
	StatusTempAuthFailure      = 454
	StatusAuthInvalid          = 535
	StatusAuthRequired         = 530
	StatusEncryptionRequired   = 538
	StatusServerError          = 550
	StatusExceedStorage        = 552


View Source
var (
	ErrPasswordNeeded       = Error{Code: StatusPasswordNeeded, Message: StatusString(StatusPasswordNeeded)}
	ErrMessageError         = Error{Code: StatusMessageError, Message: StatusString(StatusMessageError)}
	ErrMessageExceedStorage = Error{Code: StatusMessageExceedStorage, Message: StatusString(StatusMessageExceedStorage)}
	ErrTempAuthFailure      = Error{Code: StatusTempAuthFailure, Message: StatusString(StatusTempAuthFailure)}
	ErrAuthInvalid          = Error{Code: StatusAuthInvalid, Message: StatusString(StatusAuthInvalid)}
	ErrAuthRequired         = Error{Code: StatusAuthRequired, Message: StatusString(StatusAuthRequired)}
	ErrServerError          = Error{Code: StatusServerError, Message: StatusString(StatusServerError)}
	ErrExceedStorage        = Error{Code: StatusExceedStorage, Message: StatusString(StatusExceedStorage)}


func StatusString

func StatusString(status int) string


type Envelope

type Envelope struct {
	Sender     string
	Recipients []string
	Data       []byte

Envelope holds a message

func (*Envelope) AddReceivedLine

func (env *Envelope) AddReceivedLine(peer Peer)

AddReceivedLine prepends a Received header to the Data

type Error

type Error struct {
	Code    int    // The integer error code
	Message string // The error message

Error represents an Error reported in the SMTP session.

func (Error) Error

func (e Error) Error() string

Error returns a string representation of the SMTP error

type Peer

type Peer struct {
	HeloName   string               // Server name used in HELO/EHLO command
	Username   string               // Username from authentication, if authenticated
	Password   string               // Password from authentication, if authenticated
	Protocol   Protocol             // Protocol used, SMTP or ESMTP
	ServerName string               // A copy of Server.Hostname
	Addr       net.Addr             // Network address
	TLS        *tls.ConnectionState // TLS Connection details, if on TLS

Peer represents the client connecting to the server

type Protocol

type Protocol string

Protocol represents the protocol used in the SMTP session

const (
	SMTP  Protocol = "SMTP"
	ESMTP          = "ESMTP"

type Server

type Server struct {
	Hostname       string // Server hostname. (default: "localhost.localdomain")
	WelcomeMessage string // Initial server banner. (default: "<hostname> ESMTP ready.")

	ReadTimeout  time.Duration // Socket timeout for read operations. (default: 60s)
	WriteTimeout time.Duration // Socket timeout for write operations. (default: 60s)
	DataTimeout  time.Duration // Socket timeout for DATA command (default: 5m)

	MaxConnections int // Max concurrent connections, use -1 to disable. (default: 100)
	MaxMessageSize int // Max message size in bytes. (default: 10240000)
	MaxRecipients  int // Max RCPT TO calls for each envelope. (default: 100)

	// New e-mails are handed off to this function.
	// Can be left empty for a NOOP server.
	// If an error is returned, it will be reported in the SMTP session.
	Handler func(peer Peer, env Envelope) error

	// Enable various checks during the SMTP session.
	// Can be left empty for no restrictions.
	// If an error is returned, it will be reported in the SMTP session.
	// Use the Error struct for access to error codes.
	ConnectionChecker func(peer Peer) error              // Called upon new connection.
	HeloChecker       func(peer Peer, name string) error // Called after HELO/EHLO.
	SenderChecker     func(peer Peer, addr string) error // Called after MAIL FROM.
	RecipientChecker  func(peer Peer, addr string) error // Called after each RCPT TO.

	// Enable PLAIN/LOGIN authentication, only available after STARTTLS.
	// Can be left empty for no authentication support.
	Authenticator func(peer Peer, username, password string) error

	EnableXCLIENT bool // Enable XCLIENT support (default: false)

	TLSConfig *tls.Config // Enable STARTTLS support.
	ForceTLS  bool        // Force STARTTLS usage.

Server defines the parameters for running the SMTP server

package main

import (


func main() {
	var server *smtpd.Server

	// No-op server. Accepts and discards
	server = &smtpd.Server{}

	// Relay server. Accepts only from single IP address and forwards using the Gmail smtp
	server = &smtpd.Server{
		HeloChecker: func(peer smtpd.Peer, name string) error {
			if !strings.HasPrefix(peer.Addr.String(), "") {
				return errors.New("Denied")
			return nil

		Handler: func(peer smtpd.Peer, env smtpd.Envelope) error {
			return smtp.SendMail(


func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(addr string) error

ListenAndServe starts the SMTP server and listens on the address provided

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

Serve starts the SMTP server and listens on the Listener provided


Path Synopsis
Command dkim-proxy implements a simple SMTP proxy that DKIM signs incoming e-mail and relays to another SMTP server for delivery
Command dkim-proxy implements a simple SMTP proxy that DKIM signs incoming e-mail and relays to another SMTP server for delivery

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL