Skip to content

Email Processing Pipeline

When an email arrives at a ServerlessInbox domain it passes through a chain of AWS services before it lands in a user’s mailbox. This page explains that chain and the design choices behind it.

Scope: This page covers inbound email receiving only. Outbound sending and its feedback loop (bounces, complaints, delivery notifications) are covered separately.


The receiving path is built on AWS services acting as a reliable pipeline: SES → S3 and a forwarder function → SQS → Lambda.

graph LR
Internet["Internet (SMTP)"]
SES["Amazon SES<br/>Receipt Rules"]
S3_raw["S3<br/>incoming-emails/"]
Forwarder["Lambda<br/>receipt forwarder"]
SQS["SQS Queue"]
Lambda["Lambda<br/>email-processor"]
Internet -->|"SMTP (TLS required)"| SES
SES -->|"1. Store raw MIME"| S3_raw
SES -->|"2. Notify"| Forwarder
Forwarder -->|"Enqueue notification"| SQS
SQS --> Lambda

SES first writes the full email to S3, then publishes a lightweight notification — so the raw message is always durably stored before any processing begins.

SES receipt rules cannot publish to SQS directly, and their SNS notification action limits the size of the inbound message it can carry. A small forwarder function receives the notification from SES and places it on the queue, so every message takes the same path whatever its size. Full-text search indexing does not hang off inbound mail events; it runs independently on an EventBridge schedule, see Search Architecture.

ConcernMechanism
Temporary failuresThe message is retried automatically
Unknown or inactive recipientsA final outcome: processed once, recorded in the Mail log, never retried
Partial batch failuresOnly failed messages in a batch are retried, not the whole batch
Data loss on cold startRaw MIME is in S3 before Lambda is ever invoked

The Lambda receives a batch of notifications and processes each one independently:

graph TD
A["Notification arrives"]
B["Identify recipients<br/>(alias, then catch-all)"]
C["Download MIME from S3"]
D["For each routed recipient:<br/>route to inbox or junk"]
E["Store email<br/>(S3 + DynamoDB)"]
G["Record outcomes<br/>(Mail log)"]
F["Notify connected clients<br/>(WebSocket push)"]
A --> B --> C --> D --> E --> G --> F

SES delivers email to an entire domain — it does not know which tenant or account owns a given address. The processor resolves each destination address by looking it up in the database.

A few things happen during this lookup:

  • Plus-addressing — user+tag@domain.com is treated as user@domain.com for delivery purposes.
  • Alias resolution — every address is an alias pointing to an account. An exact alias match always decides the delivery.
  • Catch-all — an address with no alias is checked against the catch-all of its domain. If the domain has one, the mail goes to the catch-all account.
  • Deduplication — if multiple envelope addresses resolve to the same account, only one copy of the email is stored.

Because SES hands the processor a whole domain’s mail, address resolution is deliberately the processor’s job rather than something pushed into the SES receipt rule: aliases, plus-addressing and account ownership are product concepts that change far more often than infrastructure should. Keeping them in one place means a mailbox or alias change takes effect immediately, with no SES rule update and no redeploy.

The effect is what you would expect: mail addressed to your domain is delivered to the mailboxes it is addressed to, with aliases and plus-addressed variants arriving in the account that owns them.

Each recipient of a message is handled on its own, so one address never holds up another. Up to 200 recipients of one message are routed; recipients beyond that are not delivered.

SituationResult
Active aliasDelivered to the alias’s account
No alias, and the domain has a catch-all accountDelivered to the catch-all account
No alias, and no usable catch-allNot delivered; recorded as an unknown recipient
Alias exists but is inactiveNot delivered, and never passed on to the catch-all; recorded as an inactive alias
The account has no inbox (for a junk message: no junk folder and no inbox)Not delivered; recorded as a failed delivery
A temporary platform failure while resolving or storingThe message is received again and retried

The unknown-recipient, inactive-alias and no-inbox outcomes are final: the message is processed once and is not retried, because a retry cannot change the result. A temporary failure is different. The message stays queued and is retried automatically (see Reliability), and the other recipients of the message are still delivered. Every outcome is recorded in the Mail log. Mail addressed to a domain that no tenant owns is not recorded.

Once recipients are resolved, SES’s scanning verdicts determine where the email lands:

  • Spam detected → placed in the junk mailbox (or inbox with the junk flag if no junk mailbox is configured), per RFC 8621 §9.
  • Malware detected → filed to junk as well, by the same route and with the same flag behaviour.
  • DMARC failure under a quarantine or reject policy → filed to junk by the same route. A failure under a policy of none goes to the inbox.
  • Everything else → inbox.

Junk is the right destination for a suspect message rather than silent deletion: the recipient can see that something arrived and was set aside. The message is kept away from the inbox without a decision being made on the user’s behalf that they never get to review.

SES performs the scanning itself, at the edge, before a message ever reaches our code. See Spam & Phishing Protection for what is filtered and how to handle filtered mail.

Each recipient gets their own copy of the email stored in per-account S3 storage, plus a record in DynamoDB. Mailbox unread counters are updated atomically. After all recipients are processed, the original raw object in the incoming-emails/ S3 prefix is deleted as a best-effort cleanup.

At the end of the batch, any accumulated change notifications are pushed to connected JMAP clients via WebSocket. This is what makes new mail appear in the UI without a page refresh.