Categorygithub.com/mailhog/smtp
modulepackage
0.2.1
Repository: https://github.com/mailhog/smtp.git
Documentation: pkg.go.dev

# README

MailHog SMTP Protocol GoDoc Build Status

github.com/mailhog/smtp implements an SMTP server state machine.

It attempts to encapsulate as much of the SMTP protocol (plus its extensions) as possible without compromising configurability or requiring specific backend implementations.

proto := NewProtocol()
reply := proto.Start()
reply = proto.ProcessCommand("EHLO localhost")
// ...

See MailHog-Server and MailHog-MTA for example implementations.

Commands and replies

Interaction with the state machine is via:

  • the Parse function
  • the ProcessCommand and ProcessData functions

You can mix the use of all three functions as necessary.

Parse

Parse should be used on a raw text stream. It looks for an end of line (\r\n), and if found, processes a single command. Any unprocessed data is returned.

If any unprocessed data is returned, Parse should be called again to process then next command.

text := "EHLO localhost\r\nMAIL FROM:<test>\r\nDATA\r\nTest\r\n.\r\n"

var reply *smtp.Reply
for {
  text, reply = proto.Parse(text)
  if len(text) == 0 {
    break
  }
}

ProcessCommand and ProcessData

ProcessCommand should be used for an already parsed command (i.e., a complete SMTP "line" excluding the line ending).

ProcessData should be used if the protocol is in DATA state.

reply = proto.ProcessCommand("EHLO localhost")
reply = proto.ProcessCommand("MAIL FROM:<test>")
reply = proto.ProcessCommand("DATA")
reply = proto.ProcessData("Test\r\n.\r\n")

Hooks

The state machine provides hooks to manipulate its behaviour.

See GoDoc for more information.

HookDescription
LogHandlerCalled for every log message
MessageReceivedHandlerCalled for each message received
ValidateSenderHandlerCalled after MAIL FROM
ValidateRecipientHandlerCalled after RCPT TO
ValidateAuthenticationHandlerCalled after AUTH
SMTPVerbFilterCalled for every SMTP command processed
TLSHandlerCallback mashup called after STARTTLS
GetAuthenticationMechanismsHandlerCalled for each EHLO command

Behaviour flags

The state machine also exports variables to control its behaviour:

See GoDoc for more information.

VariableDescription
RejectBrokenRCPTSyntaxReject non-conforming RCPT syntax
RejectBrokenMAILSyntaxReject non-conforming MAIL syntax
RequireTLSRequire STARTTLS before other commands
MaximumRecipientsMaximum recipients per message
MaximumLineLengthMaximum length of SMTP line

Licence

Copyright ©‎ 2014-2015, Ian Kent (http://iankent.uk)

Released under MIT license, see LICENSE for details.

# Functions

NewProtocol returns a new SMTP state machine in INVALID state handler is called when a message is received and should return a message ID.
ParseCommand returns a Command from the line string.
ReplyAuthOk creates a 235 authentication successful reply.
ReplyAuthResponse creates a 334 authentication reply.
ReplyBye creates a 221 Bye reply.
ReplyDataResponse creates a 354 data reply.
ReplyError creates a 500 error reply.
ReplyIdent creates a 220 welcome reply.
ReplyInvalidAuth creates a 535 error reply.
ReplyLineTooLong creates a 500 Line too long reply.
ReplyMustIssueSTARTTLSFirst creates a 530 reply for RFC3207.
ReplyOk creates a 250 Ok reply.
ReplyReadyToStartTLS creates a 220 ready to start TLS reply.
ReplyRecipientOk creates a 250 Sender ok reply.
ReplySenderOk creates a 250 Sender ok reply.
ReplyStorageFailed creates a 452 error reply.
ReplySyntaxError creates a 501 Syntax error reply.
ReplyTooManyRecipients creates a 552 too many recipients reply.
ReplyUnrecognisedCommand creates a 500 Unrecognised command reply.
ReplyUnsupportedAuth creates a 504 unsupported authentication reply.

# Constants

SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.
SMTP message conversation states.

# Variables

StateMap provides string representations of SMTP conversation states.

# Structs

Command is a struct representing an SMTP command (verb + arguments).
Protocol is a state machine representing an SMTP session.
Reply is a struct representing an SMTP reply (status code + lines).

# Type aliases

State represents the state of an SMTP conversation.