# README
go-milter
A Go library to write mail filters.
Features
- With this library you can write both the client (MTA/SMTP-Server) and server (milter filter) in pure Go without sendmail's libmilter.
- Easy wrapper of the milter protocol that abstracts away many milter protocol quirks and lets you write mail filters with little effort.
- UTF-8 support
- IDNA support
- Client & Server support milter protocol version 6 with all features. E.g.:
- all milter events including DATA, UNKNOWN, ABORT and QUIT NEW CONNECTION
- milter can skip e.g. body chunks when it does not need all chunks
- milter can send progress notifications when response can take some time
- milter can automatically instruct the MTA which macros it needs.
- Automatic integration tests that test the compatibility with Postfix and Sendmail.
Installation
go get -u github.com/d--j/go-milter
Usage
The following example is a milter filter that adds [⚠️EXTERNAL]
to the subject of all messages of unauthenticated users.
See GoDoc for more documentation and an example for a milter client or a raw milter server.
package main
import (
"context"
"flag"
"log"
"strings"
"github.com/d--j/go-milter/mailfilter"
)
func main() {
// parse commandline arguments
var protocol, address string
flag.StringVar(&protocol, "proto", "tcp", "Protocol family (unix or tcp)")
flag.StringVar(&address, "addr", "127.0.0.1:10003", "Bind to address or unix domain socket")
flag.Parse()
// create and start the mail filter
mailFilter, err := mailfilter.New(protocol, address,
func(_ context.Context, trx mailfilter.Trx) (mailfilter.Decision, error) {
// Quarantine mail when it is addressed to our SPAM trap
if trx.HasRcptTo("spam-trap@スパム.example.com") {
return mailfilter.QuarantineResponse("train as spam"), nil
}
// Prefix subject with [⚠️EXTERNAL] when user is not logged in
if trx.MailFrom().AuthenticatedUser() == "" {
subject, _ := trx.Headers().Subject()
if !strings.HasPrefix(subject, "[⚠️EXTERNAL] ") {
subject = "[⚠️EXTERNAL] " + subject
trx.Headers().SetSubject(subject)
}
}
return mailfilter.Accept, nil
},
// optimization: call the decision function when all headers were sent to us
mailfilter.WithDecisionAt(mailfilter.DecisionAtEndOfHeaders),
)
if err != nil {
log.Fatal(err)
}
log.Printf("Started milter on %s:%s", mailFilter.Addr().Network(), mailFilter.Addr().String())
// wait for the mail filter to end
mailFilter.Wait()
}
License
BSD 2-Clause
Credits
Based on https://github.com/emersion/go-milter by Simon Ser which is based on https://github.com/phalaaxx/milter by Bozhin Zafirov. Max Mazurov made major contributions to this code as well.
# Packages
No description provided by the author
Package mailfilter allows you to write milter filters without boilerplate code.
Package milterutil includes utility functions and types that might be useful for writing milters or MTAs.
# Functions
AddAngle adds <> to an address.
NewClient creates a new Client object connection to a miter at network / address.
No description provided by the author
NewServer creates a new milter server.
NewTestModifier is only exported for unit-tests.
RejectWithCodeAndReason stops processing and tells client the error code and reason to sent
smtpCode must be between 400 and 599, otherwise this method will return an error.
RemoveAngle removes <> from an address.
WithAction adds action to the actions your MTA supports or your [Milter] needs.
WithActions sets the actions your MTA supports or your [Milter] needs.
WithDialer sets the [net.Dialer] this [Client] will use.
WithDynamicMilter sets the [Milter] backend this [Server] uses.
WithMacroRequest defines the macros that your [Client] intends to send at stage, or it instructs the [Server] to ask for these macros at this stage.
WithMaximumVersion sets the maximum milter version your MTA or milter filter accepts.
WithMilter sets the [Milter] backend this [Server] uses.
WithNegotiationCallback is an expert [Option] with which you can overwrite the negotiation process.
WithOfferedMaxData sets the [DataSize] that your MTA wants to offer to milters.
WithoutAction removes action from the list of actions this MTA supports/[Milter] needs.
WithoutDefaultMacros deletes all macro stage definitions that were made before this [Option].
WithoutProtocol removes protocol from the list of protocol features this MTA supports/[Milter] requests.
WithProtocol adds protocol to the protocol features your MTA should be able to handle or your [Milter] needs.
WithProtocols sets the protocol features your MTA should be able to handle or your [Milter] needs.
WithReadTimeout sets the read-timeout for all read operations of this [Client] or [Server].
WithUsedMaxData sets the [DataSize] that your MTA or milter uses to send packages to the other party.
WithWriteTimeout sets the write-timeout for all read operations of this [Client] or [Server].
# 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
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
No description provided by the author
No description provided by the author
DataSize1M is 1MB - 1 byte (command-byte).
DataSize256K is 256KB - 1 byte (command-byte).
DataSize64K is 64KB - 1 byte (command-byte).
SMFIA_INET.
SMFIA_INET6.
SMFIA_UNIX.
SMFIA_UNKNOWN.
The username of the authenticated user.
The optional overwrite username for this message.
The key length (in bits) of the used encryption layer (TLS) – if any.
The used authentication method (LOGIN, DIGEST-MD5, etc).
Validated client cert's issuer information (only when mutual TLS is in use).
Validated client cert's subject information (only when mutual TLS is in use).
Cipher suite used (set after STARTTLS or when SMTPS is used).
Strength of the cipher suite in bits (set after STARTTLS or when SMTPS is used).
Remote client IP address.
Connection concurrency for this client.
Remote client hostname.
Remote client TCP port.
Client name from address → name lookup.
Local server IP address.
name of the daemon of the MTA.
Local server TCP port.
Macros that do not have good cross-MTA support.
Macros that do not have good cross-MTA support.
Macros that do not have good cross-MTA support.
Macros that do not have good cross-MTA support.
Macros that do not have good cross-MTA support.
IP address of the interface the MTA is accepting the SMTP connection.
Interface name of the interface the MTA is accepting the SMTP connection.
the MAIL FROM address (only the address without <>).
the domain part of the MAIL FROM address.
the delivery agent for this MAIL FROM (e.g.
MTA fully qualified domain name.
Macros that do not have good cross-MTA support.
MTA Version (and MTA name in case of Postfix).
Macros that do not have good cross-MTA support.
The queue ID for this message.
the RCPT TO address (only the address without <>).
The domain part of the RCPT TO address.
MacroRcptMailer holds the delivery agent for the current RCPT TO address.
Macros that do not have good cross-MTA support.
Macros that do not have good cross-MTA support.
TLS version in use (set after STARTTLS or when SMTPS is used).
MaxClientProtocolVersion is the maximum Milter protocol version implemented by the client.
MaxServerProtocolVersion is the maximum Milter protocol version implemented by the server.
SMFIF_ADDHDRS.
SMFIF_ADDRCPT.
SMFIF_ADDRCPT_PAR [v6].
SMFIF_CHGBODY / SMFIF_MODBODY.
SMFIF_CHGFROM [v6].
SMFIF_CHGHDRS.
OptHeaderLeadingSpace lets the [Milter] request that the MTA does not swallow a leading space when passing the header value to the milter.
MTA does not send message body data.
Milter does not send a reply to body chunk event.
MTA does not send connect events.
Milter does not send a reply to connection event.
MTA does not send the DATA start event.
Milter does not send a reply to DATA start event.
MTA does not send end of header indication event.
Milter does not send a reply to end of header event.
Milter does not send a reply to header data.
MTA does not send message header data.
MTA does not send HELO/EHLO events.
Milter does not send a reply to HELO/EHLO event.
MTA does not send MAIL FROM events.
Milter does not send a reply to MAIL FROM event.
Milter does not send a reply to RCPT TO event.
MTA does not send RCPT TO events.
OptNoReplies combines all protocol flags that define that your milter does not send a reply to the MTA.
MTA does not send unknown SMTP command events.
Milter does not send a reply to unknown command event.
SMFIF_QUARANTINE.
Filter wants rejected RCPTs.
SMFIF_DELRCPT.
SMFIF_SETSYMLIST [v6].
MTA supports ActSkip.
SMFIM_CONNECT.
SMFIM_DATA.
is used for command level macros for Abort, Unknown and Header commands.
SMFIM_EOH.
SMFIM_EOM.
SMFIM_HELO.
SMFIM_ENVFROM.
identifies that a macro was not found.
SMFIM_ENVRCPT.
# Variables
No description provided by the author
ErrServerClosed is returned by the [Server]'s [Server.Serve] method after a call to [Server.Close].
LogWarning is called by this library when it wants to output a warning.
RespAccept signals to the MTA that the current transaction should be accepted.
RespContinue signals to the MTA that the current transaction should continue.
RespDiscard signals to the MTA that the current transaction should be silently discarded.
RespReject signals to the MTA that the current transaction should be rejected with a hard rejection.
RespSkip signals to the MTA that transaction should continue and that the MTA does not need to send more events of the same type.
RespTempFail signals to the MTA that the current transaction should be rejected with a temporary error code.
# Structs
No description provided by the author
Client is a wrapper for managing milter connections to one milter.
ClientSession is a connection to one Client for one SMTP connection.
MacroBag is a default implementation of the Macros interface.
Modifier provides access to [Macros] to callback handlers.
No description provided by the author
NoOpMilter is a dummy [Milter] implementation that does nothing.
Response represents a response structure returned by callback handlers to indicate how the milter server should proceed.
Server is a milter server.
# Type aliases
No description provided by the author
DataSize defines the maximum data size for milter or MTA to use.
No description provided by the author
No description provided by the author
No description provided by the author
NegotiationCallbackFunc is the signature of a [WithNegotiationCallback] function.
NewMilterFunc is the signature of a function that can be used with [WithDynamicMilter] to configure the [Milter] backend.
OptAction sets which actions the milter wants to perform.
Option can be used to configure [Client] and [Server].
OptProtocol masks out unwanted parts of the SMTP transaction.
No description provided by the author