Karta.Sending (Karta v1.0.0)

Sending through the HTTP APIs: a message built from its parts (send_message/3) or given whole (send_raw/3), authorised, stored and queued for delivery like a message submitted over SMTP.

  • Authorisation is the submission listeners': every mailbox in From and the Sender must be at a verified domain of the server's organisation (Karta.Domains.check_from/2, on the parsed header of the message as it will be sent), and so must the envelope sender (Karta.Domains.sender_allowed?/2). A suspended server sends nothing.
  • Storing follows the storage protocol of Karta.Messages: a pending row, the body in the blob store (with Karta's Received: field in front, with HTTP), then the message accepted together with its recipients and delivery jobs in one transaction (as Karta.Outbound.accept/3 does; a batch in one transaction for all). A failure on the way discards what was written and returns {:error, :temporary}.
  • Suppressed recipients (Karta.Suppressions) are recorded with the status :suppressed and not delivered to.

Both functions take the authenticated API key (Karta.Credentials, with its server) and options: :peer_ip (the client's address, for the Received: field; default 127.0.0.1), :blob_store (see Karta.Storage), :now (the time a :send_at is judged against; default: the clock, read once, so that every message of a batch is judged against the same time).

Both also take these fields, kept with the message: :tag (a label), :tags (labels: at most 10, each 1 to 128 bytes of text; the first is the tag when none is given), :variables (a map with string keys of data for the sender's own use, at most 16 KiB as JSON), :send_at (a DateTime: delivery starts then, at most 72 hours ahead; a time in the past means at once) and :tracking (%{opens: false, clicks: false} switches that tracking off for this message; true or nil leaves the server's setting, so a message never turns on what its server has off). Karta.Sending.Options checks them, and holds the batch limit.

Summary

Functions

Sends several messages built from their parts (fields as for send_message/3), all or none: each is checked, built and authorised before anything is stored, and all are accepted in one transaction. At most Karta.Sending.Options.batch_limit/0 messages. For batch sending: one message per recipient, which may share a :message_id. Returns the results in order.

Sends several messages built from their parts (fields as for send_message/3), each on its own: every message is checked, built and authorised, and those that pass are stored and accepted together, in one transaction. Returns, in order, {:ok, result} for each message sent and {:error, error} for each one refused. The whole call fails, and nothing is sent, when the server is suspended, when there are more messages than Karta.Sending.Options.batch_limit/0, or when the messages could not be stored (:temporary). For batch APIs that answer per message, whose messages are each given whole in the request (so their content is bounded by the request's body).

Builds a message and sends it. fields (atom keys)

Sends a message given whole. fields (atom keys): :mail_from (the envelope sender; "" or omitted with :auto_reply for the null sender), :rcpt_to (envelope recipients), :data (the message: header and body; lines may end in LF, they are stored with CRLF), and :tag, :tags, :variables (see the module doc). Returns the accepted message and its recipients.

Types

error()

@type error() ::
  {:invalid_address, atom()}
  | {:too_many_recipients, :to | :cc | :bcc}
  | {:invalid_header, String.t()}
  | {:invalid_attachment, non_neg_integer()}
  | {:invalid_inline, non_neg_integer()}
  | :invalid_tags
  | :invalid_variables
  | :invalid_send_at
  | :send_at_too_late
  | :invalid_tracking
  | :too_many_messages
  | :batch_too_large
  | :no_recipients
  | :no_content
  | :invalid_message
  | :message_too_large
  | :not_auto_submitted
  | :auto_reply_with_sender
  | :from_not_permitted
  | :sender_not_permitted
  | :server_suspended
  | {:quota, Karta.Quotas.reason()}
  | :temporary
  | Karta.Mail.Authorisation.reason()

result()

@type result() :: %{
  message: Karta.Messages.Message.t(),
  recipients: [Karta.Outbound.Recipient.t()]
}

Functions

send_batch(credential, batch, opts \\ [])

@spec send_batch(Karta.Credentials.Credential.t(), [map()], keyword()) ::
  {:ok, [result()]} | {:error, error()}

Sends several messages built from their parts (fields as for send_message/3), all or none: each is checked, built and authorised before anything is stored, and all are accepted in one transaction. At most Karta.Sending.Options.batch_limit/0 messages. For batch sending: one message per recipient, which may share a :message_id. Returns the results in order.

send_each(credential, batch, opts \\ [])

@spec send_each(Karta.Credentials.Credential.t(), [map()], keyword()) ::
  {:ok, ok: result(), error: error()} | {:error, error()}

Sends several messages built from their parts (fields as for send_message/3), each on its own: every message is checked, built and authorised, and those that pass are stored and accepted together, in one transaction. Returns, in order, {:ok, result} for each message sent and {:error, error} for each one refused. The whole call fails, and nothing is sent, when the server is suspended, when there are more messages than Karta.Sending.Options.batch_limit/0, or when the messages could not be stored (:temporary). For batch APIs that answer per message, whose messages are each given whole in the request (so their content is bounded by the request's body).

send_message(credential, fields, opts \\ [])

@spec send_message(Karta.Credentials.Credential.t(), map(), keyword()) ::
  {:ok, result()} | {:error, error()}

Builds a message and sends it. fields (atom keys):

  • :from (required), :sender - one address, plain (ann@example.com) or with a name (Ann <ann@example.com>), as text or already parsed (Karta.Mail.Address, as every address here);
  • :reply_to - one address, or a list of them;
  • :to, :cc, :bcc - lists of addresses, at most 50 each (option :max_recipients changes that; Bcc is not written into the message);
  • :subject, :text, :html;
  • :headers - further header fields as {name, value}; the fields Karta writes itself (From, To, Subject, Date, Message-ID, the MIME fields, Received, ...) cannot be given;
  • :message_id - the Message-ID to write (left@right, without the angle brackets); by default one is made from the message's id;
  • :attachments - maps with :content_type, :data (the bytes) and :filename;
  • :inline - parts the HTML shows (cid: URLs), maps with :content_type, :data, :content_id and :filename;
  • :tag, :tags, :variables - see the module doc.

The envelope sender is the From address; the recipients are To, Cc and Bcc. Returns the accepted message and its recipients.

send_raw(credential, fields, opts \\ [])

@spec send_raw(Karta.Credentials.Credential.t(), map(), keyword()) ::
  {:ok, result()} | {:error, error()}

Sends a message given whole. fields (atom keys): :mail_from (the envelope sender; "" or omitted with :auto_reply for the null sender), :rcpt_to (envelope recipients), :data (the message: header and body; lines may end in LF, they are stored with CRLF), and :tag, :tags, :variables (see the module doc). Returns the accepted message and its recipients.

:auto_reply (true) sends an automatic reply with the null reverse path (MAIL FROM:<>, RFC 3834 §3.3, RFC 5321 §4.5.5): :mail_from must be empty or left out, and the message itself must carry Auto-Submitted: auto-replied or auto-generated. Any message sent with an empty envelope sender needs that header, so an ordinary message cannot use the null sender to hide from bounce handling.