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.
An example
Section titled “An example”Three flows: one for the support address, one for billing, one that archives everything sent to the domain.
$schema: https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.jsonflows: 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}.emlbind: 0.0.0.0options: authOptional: trueA 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:
supportsends a CloudEvent to the support queue, withsupport@mydomain.comas its recipient;billingsends a CloudEvent to the billing queue, withbilling@mydomain.com;archivestores the email once in S3, with both addresses in itsX-smtp-relay-RCPT_TOheaders;someone@other.comwas 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.
The rules
Section titled “The rules”- Every flow sees every email. Flows are not tried in turn; any number of them can take the same email.
- 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 (awhitelistwhosetodoes not match), accepted if a filter accepts it or if the flow was already accepted earlier in the session (awhitelistonips, a successfulAUTH…), 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.
- with
- A recipient that no flow takes is refused at
RCPT TO, with550, for that recipient only. - Session filters keep their meaning. Filters on the connection,
AUTH,MAIL FROMandDATAdecide for the whole flow. A flow refused at connection (for example by awhitelistonips) cannot be reopened by a recipient that matches itstolist: it does not see the recipient at all. - After
DATA, pending recipients are settled. TheDATAfilters (http-filter,mail-auth) run; a flow they accept takes all its pending recipients, anANDflow 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 with450. - Outputs only see their flow’s recipients.
awswithsesandnodemailerdeliver 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 theX-smtp-relay-RCPT_TOheaders ofrawstorage. The CloudEventtofield is the To header of the email; when the email has no To header it lists the envelope recipients: the flow’s, unless anhttp-filterran in any flow, in which case every recipient of the email. MAIL FROMstarts over. Each new transaction on the same connection evaluates its recipients again.
Limitation: pending recipients
Section titled “Limitation: pending recipients”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:
ORflows whosetolist does not match the recipient;- flows that wait for a
DATAfilter (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.