Skip to content

Flows, filters and outputs

A configuration declares one or more flows under flows, each with a name. A flow has:

  • filters, which decide whether the flow takes the email;
  • a filtersOperator, AND (the default) or OR, which combines the filters’ answers;
  • outputs, which run, in order, for every email the flow takes.

Every flow sees every connection and every email: the flows are not tried one after the other. An email can be taken by several flows, and each of them runs its outputs.

Two flows
{
"$schema": "https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json",
"flows": {
// Emails to the support address go to a folder
"support": {
"filters": [{ "type": "whitelist", "to": ["support@example.com"] }],
"outputs": [{ "type": "file", "path": "./support/" }]
},
// Every email to example.com is logged
"audit": {
"filters": [{ "type": "whitelist", "to": ["regexp:.*@example.com"] }],
"outputs": [{ "type": "log", "fields": ["from", "to", "subject"] }]
}
},
"options": { "authOptional": true }
}

An email to support@example.com is written to ./support/ and logged; an email to sales@example.com is only logged; an email to someone@example.org is refused, because no flow accepts that recipient.

OptionTypeDescription
filtersobject[]

Filters to apply to define if the connection/email should be accepted

filtersOperator"AND" | "OR"
default "AND"

Define which operator applies if several filters are applied

outputsrequiredobject[]

Define the processors to execute on the flow

A filter looks at one or more steps of the SMTP session and, at each of them, accepts, refuses or gives no answer.

Filter Step Looks at
whitelist connection client address (ips, subnets) and hostname (domains)
whitelist MAIL FROM envelope sender (from)
whitelist RCPT TO each envelope recipient (to)
static-auth AUTH username and password, checked against the configuration
http-auth AUTH username and password, checked by an HTTP endpoint
http-filter DATA the whole email, judged by an HTTP endpoint
mail-auth DATA SPF, DKIM, DMARC, ARC and BIMI of the email

A whitelist entry that is not set gives no answer: a whitelist with only to says nothing at connection time and accepts or refuses each recipient. Each filter call is bounded by filterTimeout (30 seconds by default, see options); a filter that takes longer is stopped with an error.

At each step, the answers of the flow’s filters are combined:

  • AND (default): the flow is refused as soon as one filter refuses; it is accepted if at least one filter accepts and none refuses; with no answer it stays undecided.
  • OR: the flow is accepted as soon as one filter accepts. A refusal does not take the flow out: it stays undecided, and a later step can still accept it.

A flow refused at the connection, AUTH, MAIL FROM or DATA stays out for the rest of the connection. RCPT TO is different: it is decided for each recipient, see routing.

When no flow is left, the server answers with an error at that step: 554 at connection, 535 at AUTH, 550 at MAIL FROM and RCPT TO, 450 after DATA.

An email reaches a flow’s outputs only for the recipients the flow accepted. A recipient is accepted when a filter of the flow accepted the session before it (for example a whitelist on ips), the recipient itself (to), or the email after DATA (http-filter, mail-auth). A flow without filters, or whose filters never answer, accepts nothing: its recipients get 250 at RCPT TO and the email is then refused after DATA (or silently not delivered by that flow if another flow delivers it). Give every flow at least one filter that can accept.

Outputs (also called processors) run one after the other, in the order of the list, for each flow that takes the email. An output that fails is logged and counted in the metrics and the next outputs still run. The server answers the client’s DATA once every output has run, with 250 even if some of them failed.

Output Does
file copies the raw email to a local folder
log writes the main fields of the email to the logs
nodemailer sends the email through another SMTP server, optionally DKIM signed
aws stores it in Amazon S3, sends a CloudEvent to Amazon SQS, sends it with Amazon SES
gcp stores it in Google Cloud Storage, publishes a CloudEvent to Google Cloud Pub/Sub

Outputs only see the recipients their flow accepted: aws with ses and nodemailer deliver only to them, and the recipient variables and stored headers list only them.

Every filter and output takes an optional name, used in the logs and the metrics. It defaults to <type>_<position in the list>, for example file_0.