Karta.Outbound.Transfer (Karta v1.0.0)

One SMTP transaction with one receiving server (one address of one MX host): connect, EHLO, STARTTLS, MAIL, RCPT for each recipient, DATA.

run/4 returns either

  • {:next, outcome, info} - this server cannot take the message now (no connection, no 220 greeting, EHLO and HELO refused, a lost connection before MAIL, or TLS that the policy requires is not available): try the next address. outcome describes the failure (always temporary); or
  • {:done, results, info} - the server answered the transaction: one {status, outcome} per recipient id, status being :delivered, :deferred or :bounced.

An outcome is %{code: reply_code | nil, enhanced: "x.y.z", details: text}. With a server reply, details is the reply as the server wrote it ("550 5.1.1 No such user", lines joined) and enhanced its enhanced status code, or a default of the right class when it gave none or one of another class; without one (code: nil) Karta decided, and details says why. From MAIL on, a 4xx reply defers and a 5xx reply bounces the recipients it concerns; a connection lost from MAIL on defers every recipient not yet decided. A server that accepts the final dot and then loses the connection before its reply makes the message count as deferred, so it may arrive twice (SMTP has no way to tell).

Before MAIL the server's EHLO is checked: a message that needs SMTPUTF8 (RFC 6531 §3.4) to a server without it bounces with 5.6.7; an 8-bit message to a server without 8BITMIME bounces with 5.6.3 (no conversion); a message larger than the server's SIZE bounces with 5.3.4.

TLS (:tls option: a Karta.Outbound.TLSPolicy, or its mode alone):

  • :opportunistic (default) - STARTTLS when offered, the certificate not checked. A server that refuses STARTTLS gets the message in clear; one whose handshake fails is connected to again, once, and gets it in clear (RFC 7435).
  • :verified - STARTTLS is required, and the certificate must chain to a trusted root (:cacerts, default the operating system's) and match the MX host name (wildcards as in RFC 6125). Otherwise the server is skipped with 4.7.4 (no STARTTLS, or STARTTLS refused) or 4.7.5 (TLS failed); nothing is ever sent in clear, and a failed handshake is never followed by a connection in clear.
  • :testing - as :opportunistic, but the certificate is checked as for :verified and a failure is only reported: the message is delivered over that TLS connection all the same.
  • :encrypted - as :verified, but the certificate is not checked.

DANE (RFC 7672) comes first: when the target's MX and address answers were validated, its TLSA records are looked up (Karta.DANE.lookup/3) and decide for this server (Karta.Outbound.TLSPolicy.for_host/2). With usable records the server must authenticate with them (Karta.DANE.ssl_options/2; the certificate's names are checked against the TLSA base domain and the recipient domain, :domain); with records that are all unusable it must use TLS (:encrypted). A TLSA lookup that fails skips the server with 4.4.3 before Karta connects, as does a server without usable records when the configuration requires DANE (4.7.5).

With an MTA-STS policy in the TLS policy, the MX host must match one of its mx patterns (Karta.MTASTS.Policy.matches?/2): in enforce mode a host that does not is skipped with 4.7.5 before Karta connects; in testing mode, and for a message that says TLS-Required: No, the mismatch is only reported.

REQUIRETLS (RFC 8689 §4.2.1; the envelope's requiretls with a sender, not the null reverse path): the MX host must be authenticated, by DNSSEC (the target's mx_secure) or by the domain's MTA-STS policy naming it, or the server is skipped before Karta connects; TLS is :verified whatever the policy (TLSPolicy.require_verified/1); and after STARTTLS the server must offer REQUIRETLS, or it is skipped. Both skips are permanent: 5.7.30. The message then goes with MAIL FROM:<…> REQUIRETLS. A message of the null reverse path (a delivery status notification of such a message) is delivered as any other, with REQUIRETLS on MAIL when the session meets the same conditions (RFC 8689 §5).

info has mx_host, remote_ip, ip_used, tls_version, tls_cipher (both nil without TLS), tls_policy (the policy's name) and tls_result: for TLS reports (Karta.TLSReport), %{result:, details:, policy_type:, policy:} with "success" or the failure type of the session (an MX host the policy does not name, no STARTTLS, a certificate that failed the check, no match with the TLSA records, another TLS failure; a failed TLSA lookup and missing DANE before connecting), the type of the policy that decided ("tlsa", "sts" or "no-policy-found") and its text; or nil when no session got as far as STARTTLS.

Options: :helo (required, the name Karta gives in EHLO), :port (default 25), :tls, :cacerts, :timeouts (see Karta.SMTP.Client), :source (the local address to connect from, of the target's family; one that is not on this node fails like a refused connection: the next address is tried), :domain (the recipient domain, ASCII) and :dns (for the TLSA lookup, see Karta.DNS.lookup/3).

Summary

Functions

Text from a server, made safe to store and to quote in a bounce: printable ASCII only (anything else becomes ?), at most 900 characters.

Runs one transaction with target (%{host:, address:}). message is the bytes sent after DATA, or a Karta.Outbound.Source that reads a stored one as it is sent. See the module doc.

Types

envelope()

@type envelope() :: %{
  :mail_from => String.t(),
  :recipients => [%{id: term(), address: String.t()}],
  :smtputf8 => boolean(),
  :eight_bit => boolean(),
  optional(:requiretls) => boolean()
}

outcome()

@type outcome() :: %{code: 200..599 | nil, enhanced: String.t(), details: String.t()}

status()

@type status() :: :delivered | :deferred | :bounced

Functions

printable(text)

@spec printable(binary()) :: String.t()

Text from a server, made safe to store and to quote in a bounce: printable ASCII only (anything else becomes ?), at most 900 characters.

run(target, envelope, message, opts)

@spec run(map(), envelope(), Karta.Outbound.Source.t(), keyword()) ::
  {:next, outcome(), map()}
  | {:done, %{required(term()) => {status(), outcome()}}, map()}

Runs one transaction with target (%{host:, address:}). message is the bytes sent after DATA, or a Karta.Outbound.Source that reads a stored one as it is sent. See the module doc.