Skip to content

Routing

An email often has several recipients, and they do not all belong to the same flow. smtp-relay routes each recipient on its own: every flow judges every RCPT TO, keeps the recipients it accepted, and delivers only to them.

Three flows: one for the support address, one for billing, one that archives everything sent to the domain.

routing.yaml
$schema: https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json
flows:
support:
filters:
- type: whitelist
to: ["support@mydomain.com"]
outputs:
- type: aws
pubsub:
topic: https://sqs.us-east-1.amazonaws.com/123456789012/support
billing:
filters:
- type: whitelist
to: ["billing@mydomain.com"]
outputs:
- type: aws
pubsub:
topic: https://sqs.us-east-1.amazonaws.com/123456789012/billing
archive:
filters:
- type: whitelist
to: ["regexp:.*@mydomain.com"]
outputs:
- type: aws
storage:
bucket: mydomain-mail-archive
path: ${iso8601}_${id}.eml
bind: 0.0.0.0
options:
authOptional: true

A client sends one email with RCPT TO:<support@mydomain.com>, RCPT TO:<someone@other.com> and RCPT TO:<billing@mydomain.com>:

Recipient support billing archive Answer to RCPT TO
support@mydomain.com accepted refused accepted 250
someone@other.com refused refused refused 550
billing@mydomain.com refused accepted accepted 250

After DATA:

  • support sends a CloudEvent to the support queue, with support@mydomain.com as its recipient;
  • billing sends a CloudEvent to the billing queue, with billing@mydomain.com;
  • archive stores the email once in S3, with both addresses in its X-smtp-relay-RCPT_TO headers;
  • someone@other.com was refused, so the client knows it was not delivered; the other recipients are not affected.

The order of the RCPT TO commands makes no difference.

  1. Every flow sees every email. Flows are not tried in turn; any number of them can take the same email.
  2. Each recipient is judged on its own, by every flow. For each RCPT TO, a flow refuses the recipient, accepts it, or leaves it pending:
    • with filtersOperator: AND, the recipient is refused if a filter refuses it (a whitelist whose to does not match), accepted if a filter accepts it or if the flow was already accepted earlier in the session (a whitelist on ips, a successful AUTH…), and pending otherwise;
    • with filtersOperator: OR, the recipient is accepted if the flow was already accepted or if a filter accepts it, and pending otherwise: a filter refusing it does not refuse it.
  3. A recipient that no flow takes is refused at RCPT TO, with 550, for that recipient only.
  4. Session filters keep their meaning. Filters on the connection, AUTH, MAIL FROM and DATA decide for the whole flow. A flow refused at connection (for example by a whitelist on ips) cannot be reopened by a recipient that matches its to list: it does not see the recipient at all.
  5. After DATA, pending recipients are settled. The DATA filters (http-filter, mail-auth) run; a flow they accept takes all its pending recipients, an AND flow they refuse takes none, and any other flow keeps only the recipients it explicitly accepted. If no recipient is left in any flow, the email is refused with 450.
  6. Outputs only see their flow’s recipients. aws with ses and nodemailer deliver only to them. Outputs that store the email store it as received, and the recipient data they add lists only the flow’s recipients: the ${to} variable and the X-smtp-relay-RCPT_TO headers of raw storage. The CloudEvent to field is the To header of the email; when the email has no To header it lists the envelope recipients: the flow’s, unless an http-filter ran in any flow, in which case every recipient of the email.
  7. MAIL FROM starts over. Each new transaction on the same connection evaluates its recipients again.

A recipient left pending gets 250 at RCPT TO, because a DATA filter might still accept it. If, once the email is received, no flow accepted it, it is not delivered: the server logs No flow accepted the recipient <address>, it is not delivered, and the client is not told, as long as another recipient of the email was delivered. This happens with:

  • OR flows whose to list does not match the recipient;
  • flows that wait for a DATA filter (http-filter, mail-auth) that then refuses the email;
  • flows whose filters never answer for the recipient, such as a flow without filters.

To have unknown recipients refused at RCPT TO, give the flows AND filters that decide on the recipient, such as a whitelist with to.