Karta.Suppressions (Karta v1.0.0)

The suppression list of each server: addresses it does not send to (Karta.Suppressions.Suppression).

A message to a suppressed address is still accepted, and its other recipients are delivered as usual; the suppressed recipient is recorded with the status :suppressed and never delivered (Karta.Outbound). Refusing the recipient instead would make many SMTP clients and libraries fail the whole message.

Addresses are compared as local@domain with the domain in ASCII form, without case (normalise/1). Every lookup is scoped by server.

bounce/3 records a permanent delivery failure of an address from a receiving server's answer: the address is suppressed with the reason :hard_bounce, unless it already has an entry from somewhere else or the failure was a policy rejection of the message (5.7.x) rather than a failure of the address. complaint/3 records a spam complaint, which outranks every other reason, and soft_bounce/3 the server's soft-bounce policy (Karta.Suppressions.Policy).

The server's policy sets how long an entry of each reason lasts (expires_at) when it is added, and after how many soft bounces a :hard_bounce entry is added by itself. The policy never removes an entry: an expired one stays on the list and no longer counts (suppressed/2).

Summary

Functions

Records a permanent delivery failure of an address: the address is suppressed with the reason :hard_bounce, unless it already has an entry that came from somewhere else (:manual, :unsubscribe or :complaint), or the failure says nothing about the address.

Records a spam complaint about an address (Karta.Complaints): the address is suppressed with the reason :complaint, which outranks a bounce, an unsubscribe and an entry a person added. An entry that is already a complaint is left as it is: its expiry counts from when it became one. An entry that has expired is set again.

The server's entry for an address, or nil (also for an invalid address). With active: true, an entry that has expired is nil: it is no longer a suppression. Option :now (a DateTime) is the time the expiry is compared with.

The server's entries by address, expired ones included. Options: limit (default 100, at most 1000); reason (only entries with that reason); prefix (only addresses that start with it, without case); pattern (as prefix, but % in it stands for any text); changed_since and changed_before (DateTimes: only entries last changed at or after, or before, that time); active: true (only entries that have not expired, compared at :now); and where the page lies: offset (the number of entries to skip), after (an address: the entries after it), before (the entries just before it) or last: true (the last entries). Every page is in address order.

An envelope address (RFC 5321, UTF-8 allowed) as it is compared: local@domain with the domain in lower-case ASCII form. :error when it is not a valid address.

A page of the server's entries by address, for the admin page (Karta.Pager). Options: reason (only entries with that reason), prefix (only addresses that start with it, without case), and after, before and limit, as for Karta.Pager.page/2. Every page is in address order.

Every reason a suppression can have.

The reason each of addresses is on the server's list (expired entries included), by the address as given; addresses without an entry are left out.

Suppresses an address after the server's soft-bounce policy was reached (Karta.Suppressions.Policy.soft_bounce_reached?/3): the reason is :hard_bounce, as for a refusal, and the source says what decided ("soft_bounce_policy"). An entry of another reason is left as it is; an entry that has expired is set again.

Adds an address to the server's list, or updates its entry. attrs: address, reason (see Suppression.reasons/0), expires_at (optional). source says who or what added it.

Adds or updates several entries (attrs as for suppress/3), all or none: {:ok, count}, or {:error, index, changeset} for the first entry that is not valid.

Which of addresses (envelope recipients, as given) the server must not send to: those with an entry that has not expired.

True when a permanent failure says something about the address: an enhanced status of 5.x.x other than 5.7.x (the receiver rejected the message by policy, not the mailbox), or a three-digit 5xx reply when no enhanced code was given.

Removes an address from the server's list. Option reason: only an entry with that reason is removed.

Removes every entry of the server with this reason or, given a list of addresses, the entries of those addresses with this reason (others and invalid addresses are left alone). Returns how many.

Functions

bounce(server_id, address, opts \\ [])

@spec bounce(Ecto.UUID.t(), term(), keyword()) ::
  {:ok, Karta.Suppressions.Suppression.t()}
  | {:ok, :ignored}
  | {:error, Ecto.Changeset.t() | :invalid_address}

Records a permanent delivery failure of an address: the address is suppressed with the reason :hard_bounce, unless it already has an entry that came from somewhere else (:manual, :unsubscribe or :complaint), or the failure says nothing about the address.

Options: :status (the enhanced status code the report gave), :code (the three-digit SMTP reply), :source (what recorded it, "bounce") and :now (a DateTime, for the server's expiry). Returns {:ok, suppression}, {:ok, :ignored} when the failure is not about the address or the entry belongs to someone else, {:error, :invalid_address}, or the changeset error. An entry that has expired is set again.

complaint(server_id, address, opts \\ [])

@spec complaint(Ecto.UUID.t(), term(), keyword()) ::
  {:ok, Karta.Suppressions.Suppression.t()}
  | {:error, Ecto.Changeset.t() | :unknown_server}

Records a spam complaint about an address (Karta.Complaints): the address is suppressed with the reason :complaint, which outranks a bounce, an unsubscribe and an entry a person added. An entry that is already a complaint is left as it is: its expiry counts from when it became one. An entry that has expired is set again.

Options: :source (what recorded it, "complaint") and :now (a DateTime, for the server's expiry). Returns {:ok, suppression}, {:error, :unknown_server}, or the changeset error of an address that cannot be suppressed.

get_suppression(server, address, opts \\ [])

@spec get_suppression(Karta.Servers.Server.t(), term(), keyword()) ::
  Karta.Suppressions.Suppression.t() | nil

The server's entry for an address, or nil (also for an invalid address). With active: true, an entry that has expired is nil: it is no longer a suppression. Option :now (a DateTime) is the time the expiry is compared with.

list_suppressions(server, opts \\ [])

@spec list_suppressions(Karta.Servers.Server.t(), keyword()) :: [
  Karta.Suppressions.Suppression.t()
]

The server's entries by address, expired ones included. Options: limit (default 100, at most 1000); reason (only entries with that reason); prefix (only addresses that start with it, without case); pattern (as prefix, but % in it stands for any text); changed_since and changed_before (DateTimes: only entries last changed at or after, or before, that time); active: true (only entries that have not expired, compared at :now); and where the page lies: offset (the number of entries to skip), after (an address: the entries after it), before (the entries just before it) or last: true (the last entries). Every page is in address order.

normalise(address)

@spec normalise(term()) :: {:ok, String.t()} | :error

An envelope address (RFC 5321, UTF-8 allowed) as it is compared: local@domain with the domain in lower-case ASCII form. :error when it is not a valid address.

page_suppressions(server, opts \\ [])

@spec page_suppressions(Karta.Servers.Server.t(), keyword()) :: Karta.Pager.Page.t()

A page of the server's entries by address, for the admin page (Karta.Pager). Options: reason (only entries with that reason), prefix (only addresses that start with it, without case), and after, before and limit, as for Karta.Pager.page/2. Every page is in address order.

reasons()

@spec reasons() :: [atom()]

Every reason a suppression can have.

reasons(server_id, addresses)

@spec reasons(Ecto.UUID.t(), [String.t()]) :: %{required(String.t()) => atom()}

The reason each of addresses is on the server's list (expired entries included), by the address as given; addresses without an entry are left out.

soft_bounce(server_id, address, opts \\ [])

@spec soft_bounce(Ecto.UUID.t(), term(), keyword()) ::
  {:ok, Karta.Suppressions.Suppression.t()}
  | {:error, Ecto.Changeset.t() | :invalid_address}

Suppresses an address after the server's soft-bounce policy was reached (Karta.Suppressions.Policy.soft_bounce_reached?/3): the reason is :hard_bounce, as for a refusal, and the source says what decided ("soft_bounce_policy"). An entry of another reason is left as it is; an entry that has expired is set again.

Options: :source and :now (a DateTime).

suppress(server, attrs, source \\ nil)

@spec suppress(Karta.Servers.Server.t(), map(), String.t() | nil) ::
  {:ok, Karta.Suppressions.Suppression.t()} | {:error, Ecto.Changeset.t()}

Adds an address to the server's list, or updates its entry. attrs: address, reason (see Suppression.reasons/0), expires_at (optional). source says who or what added it.

suppress_all(server, entries, source \\ nil)

@spec suppress_all(Karta.Servers.Server.t(), [map()], String.t() | nil) ::
  {:ok, non_neg_integer()} | {:error, non_neg_integer(), Ecto.Changeset.t()}

Adds or updates several entries (attrs as for suppress/3), all or none: {:ok, count}, or {:error, index, changeset} for the first entry that is not valid.

suppressed(server_id, addresses)

@spec suppressed(Ecto.UUID.t(), [String.t()]) :: MapSet.t(String.t())

Which of addresses (envelope recipients, as given) the server must not send to: those with an entry that has not expired.

suppressible?(status, code)

@spec suppressible?(String.t() | nil, integer() | nil) :: boolean()

True when a permanent failure says something about the address: an enhanced status of 5.x.x other than 5.7.x (the receiver rejected the message by policy, not the mailbox), or a three-digit 5xx reply when no enhanced code was given.

unsuppress(server, address, opts \\ [])

@spec unsuppress(Karta.Servers.Server.t(), term(), keyword()) ::
  {:ok, Karta.Suppressions.Suppression.t()} | {:error, :not_found}

Removes an address from the server's list. Option reason: only an entry with that reason is removed.

unsuppress_all(server, reason, addresses \\ nil)

@spec unsuppress_all(Karta.Servers.Server.t(), atom(), [term()] | nil) ::
  non_neg_integer()

Removes every entry of the server with this reason or, given a list of addresses, the entries of those addresses with this reason (others and invalid addresses are left alone). Returns how many.