package module
Version: v0.2.1 Latest Latest

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

Go to latest
Published: Oct 16, 2019 License: BSD-2-Clause Imports: 12 Imported by: 0


Travis Status Code Coverage Go Report Card GoDoc


A Go library for milter support heavily inspired from andybalholm/milter For example how to use the library see phalaaxx/pf-milters - postfix milter for email classification with bogofilter and blacklisting messages which contain files with executable extensions.



Package milter A Go library for milter support



View Source
const (
	RespAccept   = SimpleResponse(acceptAction)
	RespContinue = SimpleResponse(continueAction)
	RespDiscard  = SimpleResponse(discardAction)
	RespReject   = SimpleResponse(rejectAction)
	RespTempFail = SimpleResponse(tempFailAction)

Define standard responses with no data

View Source
const (
	// OptAddHeader allow Add headers
	OptAddHeader OptAction = 0x01
	// OptChangeBody allow milter to rewrite email body
	OptChangeBody OptAction = 0x02
	// OptAddRcpt allow milter to add recipients
	OptAddRcpt OptAction = 0x04
	// OptRemoveRcpt allow milter to remove recipients
	OptRemoveRcpt OptAction = 0x08
	// OptChangeHeader allow milter to change or delete headers
	OptChangeHeader OptAction = 0x10
	// OptQuarantine allow milter to quarantine a message
	OptQuarantine OptAction = 0x20

	// OptAllEvents milter should receive all events
	OptAllEvents OptProtocol = 0x00
	// OptNoConnect milter not interested on SMTP Connect event
	OptNoConnect OptProtocol = 0x01
	// OptNoHelo milter not interested on SMTP Helo message
	OptNoHelo OptProtocol = 0x02
	// OptNoMailFrom milter not interested on email MailFrom event
	OptNoMailFrom OptProtocol = 0x04
	// OptNoRcptTo milter not interested on email RcptTo event
	OptNoRcptTo OptProtocol = 0x08
	// OptNoBody milter not interested email Body event
	OptNoBody OptProtocol = 0x10
	// OptNoHeaders  not interested about email Headers
	OptNoHeaders OptProtocol = 0x20
	// OptNoEOH not intererested on End of Headers
	OptNoEOH OptProtocol = 0x40


This section is empty.


This section is empty.


type CustomResponse

type CustomResponse struct {
	// contains filtered or unexported fields

CustomResponse is a response instance used by callback handlers to indicate how the milter should continue processing of current message

func NewResponse

func NewResponse(code byte, data []byte) *CustomResponse

NewResponse generates a new CustomResponse suitable for WritePacket

func NewResponseStr

func NewResponseStr(code byte, data string) *CustomResponse

NewResponseStr generates a new CustomResponse with string payload

func (*CustomResponse) Continue

func (c *CustomResponse) Continue() bool

Continue returns false if milter chain should be stopped, true otherwise

func (*CustomResponse) Response

func (c *CustomResponse) Response() *Message

Response returns message instance with data

type Message

type Message struct {
	Code byte
	Data []byte

Message represents a command sent from milter client

type Milter

type Milter interface {
	// Connect is called to provide SMTP connection data for incoming message
	//   suppress with NoConnect
	Connect(host string, family string, port uint16, addr net.IP, m *Modifier) (Response, error)

	// Helo is called to process any HELO/EHLO related filters
	//   suppress with NoHelo
	Helo(name string, m *Modifier) (Response, error)

	// MailFrom is called to process filters on envelope FROM address
	//   suppress with NoMailForm
	MailFrom(from string, m *Modifier) (Response, error)

	// RcptTo is called to process filters on envelope TO address
	//   suppress with NoRcptTo
	RcptTo(rcptTo string, m *Modifier) (Response, error)

	// Header is called once for each header in incoming message
	//   suppress with NoHeaders
	Header(name string, value string, m *Modifier) (Response, error)

	// Headers is called when all message headers have been processed
	//   suppress with NoHeaders
	Headers(h textproto.MIMEHeader, m *Modifier) (Response, error)

	// BodyChunk is called to process next message body chunk data (up to 64KB in size)
	//   suppress with NoBody
	BodyChunk(chunk []byte, m *Modifier) (Response, error)

	// Body is called at the end of each message
	//   all changes to message's content & attributes must be done here
	Body(m *Modifier) (Response, error)

Milter is an interface for milter callback handlers

type Modifier

type Modifier struct {
	Macros  map[string]string
	Headers textproto.MIMEHeader
	// contains filtered or unexported fields

Modifier provides access to Macros, Headers and Body data to callback handlers. It also defines a number of functions that can be used by callback handlers to modify processing of the email message

func (*Modifier) AddHeader

func (m *Modifier) AddHeader(name, value string) error

AddHeader appends a new email message header the message

func (*Modifier) AddRecipient

func (m *Modifier) AddRecipient(r string) error

AddRecipient appends a new envelope recipient for current message

func (*Modifier) ChangeHeader

func (m *Modifier) ChangeHeader(index int, name, value string) error

ChangeHeader replaces the header at the specified position with a new one

func (*Modifier) DeleteRecipient

func (m *Modifier) DeleteRecipient(r string) error

DeleteRecipient removes an envelope recipient address from message

func (*Modifier) Quarantine

func (m *Modifier) Quarantine(reason string) error

Quarantine a message by giving a reason to hold it

func (*Modifier) ReplaceBody

func (m *Modifier) ReplaceBody(body []byte) error

ReplaceBody substitutes message body with provided body

type NewMilter added in v0.2.0

type NewMilter func() Milter

type OptAction

type OptAction uint32

OptAction sets which actions the milter wants to perform. Multiple options can be set using a bitmask, which may be set by the milter in the "actions" field of the SMFIC_OPTNEG response packet.

type OptProtocol

type OptProtocol uint32

OptProtocol masks out unwanted parts of the SMTP transaction. To mask out unwanted parts (saving on "over-the-wire" data churn), the following can be set in the "protocol" field of the SMFIC_OPTNEG response packet.

type Response

type Response interface {
	Response() *Message
	Continue() bool

Response represents a response structure returned by callback handlers to indicate how the milter server should proceed

type Server

type Server struct {
	NewMilter NewMilter
	Actions   OptAction
	Protocol  OptProtocol
	// contains filtered or unexported fields

Server Milter Server

func New

func New(newMilter NewMilter, actions OptAction, protocol OptProtocol) *Server

New return Milter Server instance

func (*Server) Serve

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

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call milterSession.Handler to reply to them.

func (*Server) Shutdown

func (srv *Server) Shutdown()

Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down.

type SimpleResponse

type SimpleResponse byte

SimpleResponse type to define list of pre-defined responses

func (SimpleResponse) Continue

func (r SimpleResponse) Continue() bool

Continue to process milter messages only if current code is Continue

func (SimpleResponse) Response

func (r SimpleResponse) Response() *Message

Response returns a Message object reference

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL