Delivery, retries and bounces

Every accepted outgoing message gets one row per envelope recipient (Karta.Outbound.Recipient) and one delivery job per recipient domain (Karta.Outbound.Deliverer, Oban queue outbound, or the queue of the server's IP pool). This page follows a message from there.

Recipient states

StateMeaningPostal APIFinal
queuednot tried yetPendingno
deferredtried, failed temporarily; tried again at next_attempt_atSoftFailno
delivereda receiving server accepted the message for this recipientSentyes
bouncedrefused permanently, or still failing when the retry period endedHardFailyes
suppressedon the server's suppression list when accepted; never triedHeldyes

Each recipient keeps attempts, and the last outcome in last_code (the SMTP reply code, absent when Karta decided without a reply), last_enhanced (an RFC 3463 enhanced status code) and last_details. Every attempt is also recorded as a delivery (Karta.Outbound.Delivery): status, code, enhanced code, details, the MX host and address tried, the local address, the TLS version and cipher, and the TLS policy (opportunistic, verified, mta-sts or mta-sts-testing).

delivered means the next server accepted responsibility; it says nothing about the recipient's mailbox.

Finding the recipient's mail servers

Karta.Outbound.MXResolver follows RFC 5321 §5.1:

  • MX records, lowest preference first; hosts of equal preference in random order; at most 10 hosts.
  • No MX records: the domain itself is the mail host (implicit MX).
  • A null MX (0 ., RFC 7505): the domain accepts no mail; the recipient bounces with 5.1.10.
  • A domain that does not exist: bounce with 5.1.2.
  • Each host's IPv4 and IPv6 addresses, in the order of KARTA_OUTBOUND_IP_FAMILY (default: IPv4 first); with ipv4_only or ipv6_only the other family is not looked up.
  • Refused addresses are never connected to: those Karta.NetGuard refuses, the private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16), carrier-grade NAT (100.64.0.0/10), loopback, link-local, unique local (fc00::/7), NAT64 (64:ff9b::/96, 64:ff9b:1::/48), 6to4 (2002::/16), Teredo, documentation, multicast, reserved and the other special ranges, unless the operator allows a network (KARTA_NET_GUARD_ALLOWED_NETWORKS). A domain whose hosts have only refused addresses is deferred (4.4.4), so a hostile MX record (or an address literal) cannot make Karta connect to itself or into its own network, where a mail server might trust it.
  • An address literal (bob@[192.0.2.1], bob@[IPv6:2001:db8::1]) is the address itself.

Karta tries the addresses in order, at most 5 per run, until one answers the SMTP transaction.

The SMTP transaction

Karta connects to port 25, greets with EHLO <KARTA_OUTBOUND_HOSTNAME> (falling back to HELO), starts TLS when appropriate, and sends MAIL FROM, one RCPT TO per recipient at that domain, and DATA. A server in an IP pool connects from one of the pool's addresses and greets with that address's name instead.

  • A 4xx reply defers and a 5xx reply bounces the recipients it concerns, from MAIL FROM on.
  • No connection, no 220 greeting, EHLO and HELO refused, or a lost connection before MAIL: the next address is tried.
  • A connection lost after the final dot but before the reply counts as deferred, so that recipient may receive the message twice; SMTP cannot tell.

What the receiving server must support

Before MAIL FROM, Karta checks the server's EHLO reply:

The messageThe server lacksResult
needs SMTPUTF8 (UTF-8 addresses or header)SMTPUTF8bounce, 5.6.7 (RFC 6531)
has 8-bit content8BITMIMEbounce, 5.6.3 (Karta does not convert)
is larger than the server's SIZEroombounce, 5.3.4

A stored message with bare CR or LF line endings bounces with 5.6.0.

What is sent, and from where

The message is never held whole. Karta reads the stored body once for what the delivery needs — its size (for SIZE and the check against the server's limit), whether any octet is not ASCII (BODY=8BITMIME), whether the header block is, for SMTPUTF8 with the envelope, whether a CR or LF stands alone, and the canonical body hashes a DKIM signature covers — and then reads it again, chunk by chunk, for each address it is sent to.

Dot-stuffing (RFC 5321 §4.5.2) and the check for bare line endings are done on those chunks, so a line break, a leading dot or the CRLF before it that falls between two chunks is handled as if the message had been read whole: the bytes on the wire are the same. When a signature is made, the DKIM-Signature fields and the header block are written in front of the body; a message sent unsigned is read from its first byte. A message whose body cannot be decrypted is an error before anything is sent, not a short message.

A message the server personalises for each recipient cannot be read this way: each recipient's copy differs, so it is built whole (see per-recipient copies). The header block — at most a megabyte — is held in every delivery, since a signature is made over it.

TLS

Three modes (Karta.Outbound.TLSPolicy):

  • Opportunistic (the default): STARTTLS when the server offers it, without checking the certificate. A server that does not offer it gets the message in clear; a server whose handshake fails is connected to again, once, and gets it in clear (RFC 7435).
  • Verified: STARTTLS is required, the certificate must chain to a trusted root (the operating system's by default), be valid now and match the MX host name. Otherwise the address is skipped with 4.7.4 (no STARTTLS, or STARTTLS refused) or 4.7.5 (TLS failed), and the next address is tried. Nothing is ever sent in clear: there is no second connection without TLS after a failed handshake. When every address fails, the recipients are deferred and tried again later, like any temporary failure.
  • Testing: as opportunistic, but the certificate is checked as for verified and a failure is only reported (see TLS reports below).
  • Encrypted: as verified, but the certificate is not checked (an MX host whose TLSA records are all unusable, below).

Which mode a delivery uses, the strictest first:

  1. For each MX host, its DANE TLSA records (below), when its MX and address records were validated with DNSSEC: the server must authenticate with them; neither the domain's MTA-STS policy nor the configuration applies to that host. Recorded as dane.
  2. The recipient domain publishes an MTA-STS policy in enforce mode: verified, and the MX host must also be one the policy names (4.7.5 before Karta even connects otherwise). Recorded as mta-sts.
  3. The configuration asks for verified TLS, for all domains (:tls) or for this one (:tls_policies). Recorded as verified.
  4. The domain publishes an MTA-STS policy in testing mode. Recorded as mta-sts-testing.
  5. Otherwise opportunistic.
config :karta, Karta.Outbound,
  tls_policies: %{"bank.example" => :verified, "partner.example" => :dane}

:dane requires DANE for a domain (RFC 7672 §6): an MX host without usable, validated TLSA records is skipped with 4.7.5.

A message can change this (below): REQUIRETLS makes every delivery of it verified, and TLS-Required: No makes the domain's policies report only.

DANE

Karta.DANE follows RFC 7672 (and RFC 7671), using the DNSSEC validation of the local resolver (DNS resolver):

  • Only an MX host whose MX records (or the validated answer that the domain has none) and address records were validated has its TLSA records looked up, at _25._tcp.<host> (the port Karta delivers to), for the host's name after CNAMEs and then for the name as listed. An answer that is not validated means no DANE: the same records in an unsigned zone are ignored.
  • Usable records are of usage 2 (DANE-TA) or 3 (DANE-EE), selector 0 or 1, matching type 0, 1 (SHA-256) or 2 (SHA-512); usages 0 and 1 and anything malformed are not used, and of the digests only the strongest present counts (RFC 7671 §9).
  • With usable records the server must present a certificate that matches one: DANE-EE its own certificate or public key, its names and dates not checked; DANE-TA a certificate of its chain, from which its own certificate must chain, be current and name the MX host (the TLSA base domain) or the recipient domain. No other root is trusted. Karta sends the TLSA base domain as SNI.
  • With records that are all unusable, TLS is still required, but the certificate is not checked.
  • Under DANE nothing is sent in clear: no STARTTLS skips the address with 4.7.4, a failed handshake or a certificate that matches no record with 4.7.5 (no certificate the server presented matches the TLSA records of <host>); a TLSA lookup that fails skips it with 4.4.3 before Karta connects. The next address or MX host is tried; when none takes the message it is deferred.

REQUIRETLS and TLS-Required

A message submitted with MAIL FROM:<...> REQUIRETLS (RFC 8689; SMTP submission), or sent over /api/v2 with "require_tls": true, is delivered only:

  • to an MX host that is authenticated: its MX records were validated with DNSSEC, or the domain's MTA-STS policy names it;
  • over verified TLS (DANE, or a certificate for the MX host from a trusted root), whatever the domain's policy;
  • to a server that offers REQUIRETLS after STARTTLS, and with REQUIRETLS on its MAIL FROM, so the next server keeps the requirement.

An MX host that is not authenticated, or a server without REQUIRETLS, is skipped for good (5.7.30); when every server tried was, the recipient bounces at once. A TLS failure is temporary, as under any verified policy: the message is deferred and tried again. Its delivery status notification carries the original's header only (as for RET=HDRS) and is itself marked REQUIRETLS; it is delivered even to a server without it (RFC 8689 §5).

A message whose header has a TLS-Required: No field (exactly one; RFC 8689 §3), and no REQUIRETLS, asks that the recipient's policies not stop it: DANE and MTA-STS are checked and reported but not enforced, and the message goes in clear when the server has no STARTTLS. Recorded as tls-required-no. A domain configured :verified or :dane stays so.

MTA-STS

Karta.MTASTS follows RFC 8461. Before a delivery, Karta looks up the domain's _mta-sts TXT record. When it announces a policy version that Karta has not cached, Karta fetches https://mta-sts.<domain>/.well-known/mta-sts.txt:

  • only from public addresses: the policy host is resolved by Karta and private, loopback, link-local, carrier-grade NAT, unique local, NAT64, 6to4 and other special addresses are refused; Karta connects to the address it checked (Karta.NetGuard);
  • over TLS 1.2 or 1.3 with a certificate valid for the policy host from a trusted root;
  • only a 200 with text/plain counts; redirects are not followed;
  • within 60 seconds and 64 KiB.

A valid policy is kept in Postgres for its max_age (at most a year) and applied while it lasts, also when the TXT record or the policy host can no longer be reached: whoever blocks them cannot switch the policy off. A version that failed to fetch is not tried again for five minutes. Every hour, policies fetched more than a day ago are fetched again (Karta.MTASTS.Refresh), so a policy in use does not lapse. A policy in none mode means no policy. Turn the feature off with mta_sts: false in the Karta.Outbound configuration.

TLS reports

Every TLS session of a delivery is recorded with its result for TLS reports (RFC 8460; Karta.TLSReport): success, or the failure (starttls-not-supported, certificate-host-mismatch, certificate-expired, certificate-not-trusted, validation-failure, and for DANE tlsa-invalid, dnssec-invalid and dane-required), under the MX host's TLSA records (tlsa), the domain's MTA-STS policy (sts) or no-policy-found, with the MX host and both addresses. Failed policy fetches are recorded too (sts-policy-fetch-error, sts-webpki-invalid, sts-policy-invalid). The data is kept for 30 days; Karta sends the reports from it (Reports).

Acting on a message

An outgoing message that is still to be delivered can be acted on, by members, admins and owners on its page and by an API key with the scope messages:manage (POST /api/v2/messages/{id}/retry, hold, release and cancel). Each action is judged for every recipient and acts only on the recipients whose state it is for:

ActionActs onDoes
Try again nowqueued or deferred, not held, due later, no delivery in progressmakes it due now and queues a delivery
Holdqueued or deferred, not heldnothing is sent until it is released
Releaseheldclears the hold and queues a delivery
Cancelqueued, never tried, scheduled for laternever sent

A delivered, bounced, suppressed or cancelled recipient is final: no action touches it, so a delivered message is not sent again by accident. An action that finds nothing to act on changes nothing and says why. A held recipient is not taken by any node (delivery takes recipients with one conditional update that excludes them) and keeps its 5-day retry period: held for longer, it fails at its first attempt after the release. A delivery already in progress when a message is held finishes. Retrying never interrupts a delivery in progress. Each action is recorded with who did it (the user, or the API key's name) and shown in the message's history.

Timeouts

The SMTP client follows RFC 5321 §4.5.3.2 (Karta.SMTP.Client): connect 30 s, greeting 5 min, EHLO 5 min, STARTTLS 2 min, MAIL 5 min, RCPT 5 min, DATA 2 min, the reply after the final dot 10 min, each write of the message 3 min, QUIT 10 s. A timeout is a deadline for the whole reply.

Retries

A temporarily failed recipient is tried again after 5, 10, 20, 40, 80 and 160 minutes, then every 4 hours, until 5 days after it was queued (or after its scheduled time, if later). The last attempt is made at the 5-day mark; a temporary failure then is final: the recipient bounces with 4.4.7 and the details delivery time expired; last error: ....

From the time a recipient is queued, attempts happen at about 0, 5 min, 15 min, 35 min, 1 h 15 min, 2 h 35 min, 5 h 15 min, then every 4 hours from 9 h 15 min, and finally at 120 h: 36 attempts in all if every one fails temporarily.

Failures inside Karta never lose a recipient: an unreadable stored message is a temporary failure (4.3.0), an exception makes Oban run the job again (up to 20 times), and the sweep (Karta.Outbound.Sweeper, every 15 minutes) queues delivery again for recipients whose job is gone.

Waiting for a limit (a server's send limit, a destination's limits below, an address's warm-up) is not an attempt, and the retry period keeps running meanwhile: a recipient still waiting for a destination's or a warm-up limit when it ends bounces with 4.4.7, the limit being its last error.

Limits per destination

Receiving servers limit what they take from one sending address: too many connections at once, or too many messages a minute, get 421 and 4xx replies and, when it goes on, a worse reputation. Karta keeps below such limits when KARTA_OUTBOUND_DESTINATION_LIMITS says them (an IP pool may have its own destination_limits instead):

KARTA_OUTBOUND_DESTINATION_LIMITS="*.mx.example.com connections=5 per_minute=300, *.mx.example.org connections=10, * connections=20"
  • An entry names MX hosts, then their limits: connections (open at once) and per_minute (recipients in any 60 seconds), either or both. *.mx.example.com is every host under mx.example.com, all counted together: a provider's many mail servers, which also serve its customers' domains, are one destination. A host name is that host only. * is every other host, each counted on its own. The first entry that matches decides; an entry without limits (mx.example.net) exempts its hosts. Nothing is limited by default.
  • A delivery's destination is the recipient domain's most preferred MX host. The delivery holds one connection slot for its whole run (its transactions follow one another) and takes as many recipients as the minute has room for; the others go in a following run.
  • Counts are kept on each node, per IP pool: receiving servers count per sending address, and a pool's addresses are used only on the node that holds them. Without a pool, each node counts for its own address. A node starts counting afresh when it starts.
  • A delivery held back makes no attempt and waits: 5 seconds for a connection slot, until the minute has room for recipients.
  • A pool's destination_limits replace these for its mail; "*" there lifts every limit for the pool.

Bounces

Karta records a bounce when the receiving server refuses a recipient during the SMTP transaction (5xx), when Karta decides the recipient cannot be delivered (the codes above), or when the retry period ends.

A permanent failure of the recipient (5xx from the receiving server, or a 5.x.x report, except a 5.7.x policy rejection of the message) also puts the address on the server's suppression list, with the reason hard_bounce; an entry that came from someone else is left alone. A temporary failure does not.

Asynchronous bounces (a report sent later by a server that first accepted the mail) come back to the per-recipient return path; the mx listener reads and records them (Receiving mail). Without KARTA_RETURN_PATH_DOMAIN they have nowhere to arrive, and only the synchronous outcomes are recorded.

Delivery status notifications

For a message submitted over SMTP or the APIs with a non-empty envelope sender, Karta sends a delivery status notification (RFC 3464) to that sender:

  • when a recipient bounces (unless the submitter asked for NOTIFY=NEVER or for notices without FAILURE);
  • when a recipient is delivered and the submitter asked for NOTIFY=SUCCESS.

Since the DSN extension is not advertised yet, SMTP clients cannot give NOTIFY, so in practice a DSN is sent for every bounced recipient. The DSN is a multipart/report with a text part, a message/delivery-status part and the original header (or, with RET=FULL and at most 1 MiB, the whole message); internationalised messages use the RFC 6533 forms. It comes from MAILER-DAEMON@<KARTA_OUTBOUND_HOSTNAME> with an empty envelope sender, is stored and delivered like any message of the same server, and appears in its message lists. A delivered recipient is reported with the action relayed, because Karta does not pass DSN requests on.

Automatic replies and the null reverse path

An automatic reply (a vacation answer, a challenge, any response nobody wrote) must be sent with an empty envelope sender (MAIL FROM:<>, RFC 3834 §3.3, RFC 5321 §4.5.5) so that a delivery failure can never produce a bounce. On the raw send (POST /api/v2/messages/raw and POST /kantoor/v1/messages/raw) ask for it with auto_reply: true and leave mail_from empty or out. The message itself must carry Auto-Submitted: auto-replied or auto-generated; a raw send with an empty envelope sender without that field is refused (422 not_auto_submitted), so ordinary mail cannot use the null sender to hide from bounce handling. auto_reply with a mailbox envelope sender is refused too (422 auto_reply_with_sender).

For such a message:

  • Karta sends MAIL FROM:<>, and with KARTA_RETURN_PATH_DOMAIN set it still makes no VERP return path for it (Return paths below): an asynchronous bounce, complaint or report has no address of ours to come back to.
  • Synchronous outcomes are recorded as usual: a 5xx bounces the recipient (and suppresses the address), a 4xx defers it and the retry policy applies, and the delivery log and the mail metrics count the attempt.
  • No delivery status notification is made for it, even when a recipient bounces (RFC 5321 §6.1). A DSN that Karta itself sends also has the null sender.
  • Suppressions and every rate limit still apply, as for any message.
  • DKIM is unchanged: the message is signed with its From domain's key.
  • Receivers judge SPF on the HELO name for a null reverse path (RFC 7208 §2.4), not on the empty sender. KARTA_OUTBOUND_HOSTNAME must therefore be a name whose SPF record passes; DMARC can still align through the DKIM signature over the From domain.

Return paths (VERP)

By default the envelope sender of a delivery is the message's own. With KARTA_RETURN_PATH_DOMAIN set, each recipient gets its own envelope sender (Karta.Outbound.ReturnPath):

b<recipient id in base32><tag in base32>@<return path domain>

40 characters of a-z and 2-7, where the tag is an HMAC of the recipient id with a key derived from the vault key, so a forged bounce for a guessed id is recognised as not Karta's. Each recipient then gets its own SMTP transaction. Messages with an empty envelope sender (such as DSNs) keep it empty. The domain's MX records must lead back to Karta: that is where the reports about these deliveries arrive (Receiving mail).

Per-recipient copies

When the server tracks opens or clicks or adds List-Unsubscribe, every recipient gets a copy of its own (its own tokens) and its own SMTP transaction. The copy is signed after it is personalised, so the DKIM signature covers what the recipient receives.

Duplicates

Karta writes the outcome of every recipient right after its transaction. A crash between the receiving server's acceptance and that write means the next run delivers that recipient again: at most one duplicate.

A run claims the recipients it sends before it starts: one conditional update moves their next attempt an hour forward, longer than a run may take. A second delivery job for the same message and domain — a duplicate, a sweep racing the job — takes nothing and waits. When a node is killed, the claim runs out and Oban's lifeline rescues its job after rescue_after (60 minutes), so the wait before the retry is bounded: a recipient is never lost, and only the transaction that was in flight can be sent again.