Documentation ¶
Index ¶
- Constants
- Variables
- func InitPseudoRandomNumberGeneratorFallback()
- func NewMessageID() string
- type Auth
- type AuthCramMd5
- type AuthExtension
- type AuthPlain
- type AuthUser
- type Conn
- func (c *Conn) AddInfoHeader(headerName, headerText string)
- func (c *Conn) EndTX() error
- func (c *Conn) ReadData() (string, error)
- func (c *Conn) ReadLine() (string, error)
- func (c *Conn) ReadSMTP() (string, string, error)
- func (c *Conn) Reset()
- func (c *Conn) ResetBuffers()
- func (c *Conn) StartTX(from *mail.Address) error
- func (c *Conn) WriteEHLO(message string) error
- func (c *Conn) WriteOK() error
- func (c *Conn) WriteSMTP(code int, message string) error
- type Extension
- type LimitedReader
- type Message
- type MessageHandler
- type Part
- type RcptHandler
- type SMTPError
- type Server
- func (s *Server) Address() string
- func (s *Server) Close() error
- func (s *Server) Disable(verbs ...string)
- func (s *Server) Enable(verbs ...string)
- func (s *Server) Extend(verb string, extension Extension) error
- func (s *Server) GetAddressArg(argName string, args string) (*mail.Address, error)
- func (s *Server) Greeting(conn *Conn) string
- func (s *Server) HandleSMTP(conn *Conn) error
- func (s *Server) ListenAndServe(addr string) error
- func (s *Server) SetHelp(message string) error
- func (s *Server) UseAuth(auth Extension)
- func (s *Server) UseTLS(cert, key string) error
- type SimpleAuthFunc
- type SimpleExtension
Constants ¶
const ( DefaultReadTimeout = time.Second * 10 DefaultWriteTimeout = time.Second * 10 DefaultMessageSizeMax = 131072 DefaultSessionCommandsMax = 100 )
Default values
const OK string = "OK"
Variables ¶
var ( ErrAlreadyRunning = errors.New("This server is already listening for requests") ErrAuthFailed = SMTPError{535, errors.New("Authentication credentials invalid")} ErrAuthCancelled = SMTPError{501, errors.New("Cancelled")} ErrRequiresTLS = SMTPError{538, errors.New("Encryption required for requested authentication mechanism")} ErrTransaction = SMTPError{501, errors.New("Transaction unsuccessful")} )
Well-defined errors
Functions ¶
func InitPseudoRandomNumberGeneratorFallback ¶
func InitPseudoRandomNumberGeneratorFallback()
when crypto source is exhausted, fallback to PRNG, which must have a unique seed
func NewMessageID ¶
func NewMessageID() string
NewMessageID generates a message ID, but make sure to seed the random number generator. It follows the Mailsac makeId pattern.
Types ¶
type Auth ¶
type Auth struct {
Mechanisms map[string]AuthExtension
}
type AuthCramMd5 ¶
func (*AuthCramMd5) CheckResponse ¶
func (a *AuthCramMd5) CheckResponse(response string, challenge []byte) (AuthUser, bool)
Note: This is currently very weak & requires storing of the user's password in plaintext one good alternative is to do the HMAC manually and expose handlers for pre-processing the password MD5s
func (*AuthCramMd5) Handle ¶
func (a *AuthCramMd5) Handle(conn *Conn, params string) (AuthUser, error)
Handles the negotiation of an AUTH CRAM-MD5 request https://en.wikipedia.org/wiki/CRAM-MD5 http://www.samlogic.net/articles/smtp-commands-reference-auth.htm
type AuthExtension ¶
http://tools.ietf.org/html/rfc4422#section-3.1 https://en.wikipedia.org/wiki/Simple_Authentication_and_Security_Layer
type AuthPlain ¶
type AuthPlain struct {
Auth SimpleAuthFunc
}
type Conn ¶
type Conn struct { // ID is this connection ID which changes after TLS connection ID string // optional hostname given during NAME ClientHostname string // Conn is primarily a wrapper around a net.Conn object net.Conn ForwardedForIP string // Track some mutable for this connection IsTLS bool Errors []error User AuthUser FromAddr *mail.Address ToAddr []*mail.Address // any additional text information here, like custom headers you will later prepend when passing along to another server AdditionalHeaders string // Configuration options MaxSize int64 ReadTimeout time.Duration WriteTimeout time.Duration Logger *log.Logger DiscardBody bool // contains filtered or unexported fields }
Conn is a wrapper for net.Conn that provides convenience handlers for SMTP requests
func (*Conn) AddInfoHeader ¶
AddInfoHeader adds an additional header to the beginning of the list, such that the newest headers will be at the top
func (*Conn) ReadSMTP ¶
ReadSMTP pulls a single SMTP command line (ending in a carriage return + newline)
func (*Conn) ResetBuffers ¶
func (c *Conn) ResetBuffers()
ResetBuffers resets the mail buffers (to, from) but not auth
func (*Conn) WriteEHLO ¶
WriteEHLO writes an EHLO line, see https://tools.ietf.org/html/rfc2821#section-4.1.1.1
type LimitedReader ¶
type LimitedReader struct { R io.Reader // underlying reader N int64 // max bytes remaining ReadsRemaining int DidHitLimit bool }
LimitedReader keeps from reading past the suggested max size. It was copied from io and altered to return a SMTPError.
type Message ¶
type Message struct { Conn *Conn To []*mail.Address From *mail.Address Header mail.Header Subject string RawBody []byte Source []byte MessageID string Rcpt []*mail.Address // meta info Logger *log.Logger }
Message is a nicely packaged representation of the received message
func NewMessage ¶
func NewMessage(conn *Conn, data []byte, rcpt []*mail.Address, logger *log.Logger) (*Message, error)
NewMessage creates a Message from a data blob and a recipients list
func (*Message) Attachments ¶
Attachments returns the list of attachments on this message XXX: this assumes that the only mimetype supporting attachments is multipart/mixed need to review https://en.wikipedia.org/wiki/MIME#Multipart_messages to ensure that is the case
func (*Message) FindBody ¶
FindBody finds the first part of the message with the specified Content-Type
type MessageHandler ¶
MessageHandler functions handle application of business logic to the inbound message
type Part ¶
type Part struct { Header textproto.MIMEHeader Body []byte Children []*Part // contains filtered or unexported fields }
Part represents a single part of the message
type RcptHandler ¶
type SMTPError ¶
SMTPError is an error + SMTP response code
type Server ¶
type Server struct { Name string TLSConfig *tls.Config ServerName string // MaxSize of incoming message objects, zero for no cap otherwise // larger messages are thrown away MaxSize int64 // MaxConn limits the number of concurrent connections being handled MaxConn int // MaxCommands is the maximum number of commands a server will accept // from a single client before terminating the session MaxCommands int // RateLimiter gets called before proceeding through to message handling // TODO: Implement RateLimiter func(*Conn) bool OnRcpt RcptHandler // Handler is the handoff function for messages Handler MessageHandler // Auth is an authentication-handling extension Auth Extension // Extensions is a map of server-specific extensions & overrides, by verb Extensions map[string]Extension // Disabled features Disabled map[string]bool // help message to display in response to a HELP request Help string // Logger to print out status info // TODO: implement better logging with configurable verbosity Logger *log.Logger Verbose bool // Timeout handlers ReadTimeout time.Duration WriteTimeout time.Duration // Ready is a channel that will receive a single `true` when the server has started Ready chan bool PreAuthVerbsAllowed []string // DiscardBody will read all message body text and discard it DiscardBody bool // contains filtered or unexported fields }
Server is an RFC2821/5321 compatible SMTP server
func NewServerWithLogger ¶
NewServerWithLogger creates a server with a customer logger
func (*Server) GetAddressArg ¶
GetAddressArg extracts the address value from a supplied SMTP argument for handling MAIL FROM:address@example.com and RCPT TO:address@example.com XXX: don't like this, feels like a hack
func (*Server) Greeting ¶
Greeting is a humanized response to EHLO to precede the list of available commands
func (*Server) HandleSMTP ¶
HandleSMTP handles a single SMTP request
func (*Server) ListenAndServe ¶
ListenAndServe starts listening for SMTP commands at the supplied TCP address