Karta.Outbound.Deliverer (Karta v1.0.0)

The delivery job (Oban worker): delivers one message to its recipients at one domain. It runs in the queue of the message's server's IP pool (Karta.IPPools.queue/1; outbound without a pool); a job that finds itself in another queue (the server's pool changed) queues itself again in the right one and ends.

A run:

  1. Loads the message and its recipients at the domain that are :queued or :deferred. Nothing to do: done. Recipients whose next attempt is still in the future: the job waits for the first of them ({:snooze, seconds}).
  2. Suspension and rate limit. While the message's server is suspended (Karta.Servers.suspend/2), the job waits a minute and looks again: nothing is tried, counted or dropped, and delivery goes on once the server is resumed. When the server has a send_limit_per_minute and has made that many delivery attempts in the last minute, the job waits a minute and tries again. Nothing is held, and no attempt is counted.
  3. Reads the stored message.
  4. Resolves the domain's mail servers (Karta.Outbound.MXResolver) and the TLS policy for the domain (Karta.Outbound.TLSPolicy.resolve/2: configuration and the domain's MTA-STS policy), and tries the servers' addresses in order, at most :max_targets of them, until one answers the transaction (Karta.Outbound.Transfer, which looks up each server's TLSA records first: DANE). A message whose header says TLS-Required: No (exactly one such field; RFC 8689 §3) has the recipient's policies only reported, unless it also asked for REQUIRETLS; a message that asked for REQUIRETLS goes only to servers that meet RFC 8689 (see Karta.Outbound.Transfer). The result of each TLS session is recorded for TLS reports (Karta.TLSReport). With a return path domain configured, each recipient gets its own VERP return path (Karta.Outbound.ReturnPath) and so its own transaction; the null reverse path stays null, and so does the SRS sender of a forward (Karta.Inbound.SRS). When every server tried refused the message for good before MAIL (REQUIRETLS: 5.7.30) the recipients bounce at once; otherwise they are deferred with the last temporary failure. When the message's server tracks opens or clicks or adds List-Unsubscribe (Karta.Tracking), each recipient gets its own copy and its own transaction too. The message is signed (Karta.Outbound.Signing) last, so the signature covers every change made for a recipient.
  5. Writes the outcome of every recipient before anything else happens: one Karta.Outbound.Delivery row per recipient, and the recipient's new state: delivered; bounced; or deferred until the time Karta.Outbound.RetryPolicy gives, and bounced when the retry period is over. In the same transaction it queues the delivery status notification, if one is due (below). After the commit the change is announced on the topics of the message and of its server (:recipients, Karta.Changes), and the webhook events of the outcomes (message.delivered, message.deferred, message.bounced) are recorded in the same transaction (Karta.Webhooks).
  6. Waits for the next attempt of any recipient still deferred, or ends.

IP pools (Karta.IPPools). The mail of a server in a pool goes from the pool's addresses that are on this node (the :local_addresses option, or the node's interfaces): one address per IP family, chosen by the message, is bound for every connection of the run, and EHLO gives that address's name. Only the pool's address families are tried at the recipient's mail servers (Karta.IPPools.ip_family/2), and only those of them this node holds. When the node holds none of the pool's addresses, the job waits a minute and looks again, without an attempt: the mail waits for a node that holds one. A server without a pool sends from the node's own address, with :hostname in EHLO.

Limits (Karta.Outbound.Limits). Once the recipient's mail servers are known, and before the message is read, the run takes a connection slot and its recipients from the destination's limits (Karta.Outbound.Throttle: connections at once and recipients per minute, by group of MX hosts) and reserves its recipients on the addresses it will send from: an address under a warm-up plan is used for no more recipients a day than the plan allows (Karta.Outbound.Warmup); when the message's address has no room left, another held address of its family with room is used. Recipients held back make no attempt and wait (a few seconds for a connection slot, until the minute has room, until the next UTC day for warm-up, looking again every hour); a recipient whose retry period ends while it waits bounces (4.4.7), the limit being its last error.

Delivery status notifications (Karta.Outbound.DSN) go to the envelope sender of an outgoing (submitted) message, never to the null reverse path: for a recipient that bounced, unless it asked for NOTIFY=NEVER or for notices without FAILURE; for a delivered recipient that asked for NOTIFY=SUCCESS. The notification of a REQUIRETLS message requires TLS too, and carries only the original's header (RFC 8689 §5). The DSN is stored like any message (pending row, then its body) before the transaction and accepted with its own delivery job inside it. If it cannot be stored, the outcome is still written and the failure logged.

One run at a time. Before anything is sent, a run takes the recipients it will send with one conditional update (Karta.Claim): their next_attempt_at moves :claim seconds on (default one hour, longer than this job's 30-minute limit), and only the rows that update returned are sent. A second job for the same message and domain — a duplicate, a replay, or a sweep racing the job — takes nothing and waits or ends. A run that crashes leaves its recipients to be taken again when the claim runs out; Oban's lifeline rescues the job of a dead node after its rescue_after (60 minutes).

Failures inside Karta (an unreadable stored message, an exception) never lose a recipient: an unreadable message is a temporary failure like any other, and an exception makes Oban run the job again, at most 20 times. A crash between a server's acceptance and step 5 means the next run delivers that recipient again: a message can be duplicated, never lost, and the claim bounds how long before the retry.

Options (config :karta, Karta.Outbound; deliver/3 takes overrides):

  • :hostname (required) - Karta's name: EHLO, DSN Reporting-MTA.
  • :return_path_domain - the VERP domain; nil (default) sends with the message's own envelope sender.
  • :port (25), :tls and :tls_policies (Karta.Outbound.TLSPolicy), :cacerts, :timeouts - for Karta.Outbound.Transfer.
  • :mta_sts - options for Karta.MTASTS.policy/2 (the policy fetch's :timeout, :cacerts, :allowed_networks, ...; :dns is shared), or false to read no MTA-STS policies. Default [].
  • :ip_family, :blocked_networks, :allowed_networks, :dns, :max_hosts - for Karta.Outbound.MXResolver (:dns also for the TLSA lookups).
  • :max_targets - addresses tried per run (default 5).
  • :destination_limits - limits per destination (Karta.Outbound.Throttle rules, KARTA_OUTBOUND_DESTINATION_LIMITS) for servers without an IP pool and pools without their own; default none. :throttle - the server that counts them (default Karta.Outbound.Throttle).
  • :local_addresses - this node's addresses, for IP pools (default those of its network interfaces, Karta.IPPools.local_addresses/1).
  • :max_age - the retry period in seconds (default 5 days).
  • :claim - seconds a recipient is claimed for (default one hour), longer than the job's time limit; tests shorten it.
  • :now - the time of the run (default the clock's), for tests.
  • :signing - the DKIM key source (Karta.Outbound.Signing).
  • :blob_store - overrides the message store (Karta.Storage).

Summary

Functions

One run for the recipients of message_id at domain. See the module doc. :queue in overrides is the queue the job ran in (none: not checked), :oban its Oban instance (whose monitor knows this node's addresses, Karta.IPPools.local_addresses/1).

Functions

deliver(message_id, domain, overrides \\ [])

@spec deliver(Ecto.UUID.t(), String.t(), keyword()) :: :ok | {:snooze, pos_integer()}

One run for the recipients of message_id at domain. See the module doc. :queue in overrides is the queue the job ran in (none: not checked), :oban its Oban instance (whose monitor knows this node's addresses, Karta.IPPools.local_addresses/1).