Karta.Inbound.Dispatcher (Karta v1.0.0)

The job (Oban worker, queue inbound) that delivers received mail to a route's endpoint (Karta.Inbound.Delivery): one recipient to an HTTP endpoint, or every recipient of the message to an SMTP or address endpoint at once.

A run:

  1. Loads the delivery with its message and endpoint. One that is gone or no longer :pending is done; one not yet due waits ({:snooze, seconds}).
  2. Claims what it sends: the delivery itself (an HTTP endpoint, or a removed endpoint), or every pending, due delivery of the same message to the same SMTP or address endpoint. The claim moves their next_attempt_at 30 minutes on, in one statement: a second run of the same deliveries at the same time claims nothing and ends, and a run that crashed leaves them to be tried again when the claim runs out. A removed endpoint fails its deliveries.
  3. Reads the stored message (a failure is tried again) and sends it:
    • to an HTTP endpoint through Karta.NetGuard.HTTP, as Karta.Inbound.HTTPRequest says (no private or special addresses unless the operator allows them, no redirects, at most timeout (default 30 seconds) and messages of at most max_size bytes (default 25 MiB; a notify-only endpoint is sent no message, so the limit does not apply to it)), once per recipient; the endpoint's extra authentication header, if it has one, is sent beside the format's signature (Karta.RequestAuth);
    • to an SMTP endpoint (Karta.Outbound.Transfer) at the addresses of its host that Karta.NetGuard allows, with the envelope the message arrived with: one transaction with every claimed recipient, each delivered, tried again or failed by the server's answer to its RCPT; no address answering: all tried again;
    • to an address endpoint, as one forward for every claimed recipient: an outgoing message of the same server to the endpoint's addresses, with the sender rewritten by SRS (Karta.Inbound.SRS; no SRS domain: failed) and a Delivered-To: field for each of those recipients in front (the loop guard of the mx listener stops it when it comes back to any of them), sealed with an ARC set when the message was checked on arrival and the domain it was received at has an RSA key (Karta.Inbound.Seal), and queued for delivery (Karta.Outbound) in one transaction with the deliveries' new state. The forward is the message as received: no tracking, no notifications to the sender (NOTIFY=NEVER), and its delivery keeps the SRS sender (no VERP).
  4. Records the outcome on each delivery: delivered; failed; or tried again after 1, 5, 15 and 30 minutes, 1, 2, 4, 8, 12 and 16 hours and a day (each wait 10% shorter or longer at random, the same for the deliveries of one run): 12 attempts over about three days, and failed after the last.

Nothing is ever sent to the sender when a delivery fails: the sender of received mail is not known to be genuine, and a bounce to a forged sender is backscatter. Owners see failed deliveries through the API.

Configuration (config :karta, Karta.Inbound.Dispatcher): timeout, max_size. run/2 takes them as options, with :now, :http (options for Karta.NetGuard.HTTP), :smtp (:dns, :allowed_networks, :cacerts, :timeouts) and :blob_store for tests.

Summary

Functions

The number of attempts a delivery gets.

One run for the delivery id (see the module doc).

Functions

max_attempts()

@spec max_attempts() :: pos_integer()

The number of attempts a delivery gets.

run(id, opts \\ [])

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

One run for the delivery id (see the module doc).