Lettermint.EmailBuilder (Lettermint v2.0.0)

Copy Markdown View Source

An immutable email builder for the pipe operator. Start one with Lettermint.Emails.compose/2.

Every function returns a new builder and leaves the one it was given unchanged, so a base builder can be kept and reused as a template, also across processes. to/2, cc/2, bcc/2 and reply_to/2 replace their list; attach/2 appends.

alias Lettermint.EmailBuilder, as: Email

welcome =
  lettermint
  |> Lettermint.Emails.compose()
  |> Email.from("Acme <hello@acme.com>")
  |> Email.subject("Welcome to Acme")
  |> Email.tags([%{name: "campaign", value: "welcome"}])

welcome |> Email.to("jane@example.com") |> Email.html("<p>Hi Jane</p>") |> Email.send()
welcome |> Email.to("john@example.com") |> Email.html("<p>Hi John</p>") |> Email.send(idempotency_key: "welcome-john")

A setter that gets invalid tags or attachments raises Lettermint.ClientValidationError; the builder it was given stays valid. inspect/1 and Jason.encode/1 show the message, never the client's tokens.

Summary

Types

An attachment: content is base64 text or {:bytes, binary}.

t()

Functions

Adds an attachment: a map or keyword list with :filename, :content, and optionally :content_type (a MIME type, detected by the API when left out) and :content_id (for inline images referenced as cid:<content_id>). content is base64 text or {:bytes, binary} with raw bytes.

Replaces the BCC recipients. Takes one address or a list.

The message in the API's wire format (string keys), with attachment bytes base64-encoded. Contains no credentials.

Replaces the CC recipients. Takes one address or a list.

The sender, for example Acme <hello@acme.com>.

Replaces the custom email headers.

The HTML body. nil removes it.

Replaces the metadata (stored with the message, not added as headers).

Replaces the Reply-To addresses. Takes one address or a list.

The route slug to send through.

The result that a Sandbox project simulates for every recipient, for example "hard_bounced".

Schedules delivery. Strings are passed through (ISO 8601 or English, such as "tomorrow 9am"); a DateTime is sent as ISO 8601. nil removes it.

Sends a snapshot of this email. The builder is unchanged and can be sent again. Takes the options of Lettermint.Emails.send/3.

Per-email settings that override the route settings (track_opens, track_clicks, tls).

The subject line.

The legacy single tag. nil removes it.

Replaces the name/value tags: up to 20, or 19 with a legacy tag/2. Takes maps with name and value (atom or string keys) or Lettermint.Types.MessageTagInput structs.

The plain-text body. nil removes it.

Replaces the recipients. Takes one address or a list.

Types

attachment()

@type attachment() :: %{
  :filename => String.t(),
  :content => String.t() | {:bytes, binary()},
  optional(:content_type) => String.t(),
  optional(:content_id) => String.t()
}

An attachment: content is base64 text or {:bytes, binary}.

t()

@type t() :: %Lettermint.EmailBuilder{client: Lettermint.Client.t(), message: map()}

Functions

attach(builder, attachment)

@spec attach(t(), attachment() | keyword()) :: t()

Adds an attachment: a map or keyword list with :filename, :content, and optionally :content_type (a MIME type, detected by the API when left out) and :content_id (for inline images referenced as cid:<content_id>). content is base64 text or {:bytes, binary} with raw bytes.

Email.attach(builder, filename: "invoice.pdf", content: {:bytes, pdf}, content_type: "application/pdf")

bcc(builder, addresses)

@spec bcc(t(), String.t() | [String.t()]) :: t()

Replaces the BCC recipients. Takes one address or a list.

build(email_builder)

@spec build(t()) :: map()

The message in the API's wire format (string keys), with attachment bytes base64-encoded. Contains no credentials.

cc(builder, addresses)

@spec cc(t(), String.t() | [String.t()]) :: t()

Replaces the CC recipients. Takes one address or a list.

from(builder, address)

@spec from(t(), String.t()) :: t()

The sender, for example Acme <hello@acme.com>.

headers(builder, headers)

@spec headers(t(), %{optional(String.t() | atom()) => String.t()}) :: t()

Replaces the custom email headers.

html(builder, html)

@spec html(t(), String.t() | nil) :: t()

The HTML body. nil removes it.

metadata(builder, metadata)

@spec metadata(t(), %{optional(String.t() | atom()) => String.t()}) :: t()

Replaces the metadata (stored with the message, not added as headers).

reply_to(builder, addresses)

@spec reply_to(t(), String.t() | [String.t()]) :: t()

Replaces the Reply-To addresses. Takes one address or a list.

route(builder, route)

@spec route(t(), String.t()) :: t()

The route slug to send through.

sandbox_result(builder, result)

@spec sandbox_result(t(), Lettermint.Types.SandboxResult.t() | atom()) :: t()

The result that a Sandbox project simulates for every recipient, for example "hard_bounced".

scheduled_at(builder, when_)

@spec scheduled_at(t(), String.t() | DateTime.t() | nil) :: t()

Schedules delivery. Strings are passed through (ISO 8601 or English, such as "tomorrow 9am"); a DateTime is sent as ISO 8601. nil removes it.

send(email_builder, options \\ [])

Sends a snapshot of this email. The builder is unchanged and can be sent again. Takes the options of Lettermint.Emails.send/3.

settings(builder, settings)

@spec settings(t(), map() | Lettermint.Types.SendMailRequestSettings.t()) :: t()

Per-email settings that override the route settings (track_opens, track_clicks, tls).

subject(builder, subject)

@spec subject(t(), String.t()) :: t()

The subject line.

tag(builder, tag)

@spec tag(t(), String.t() | nil) :: t()

The legacy single tag. nil removes it.

tags(builder, tags)

@spec tags(t(), [map() | Lettermint.Types.MessageTagInput.t()]) :: t()

Replaces the name/value tags: up to 20, or 19 with a legacy tag/2. Takes maps with name and value (atom or string keys) or Lettermint.Types.MessageTagInput structs.

text(builder, text)

@spec text(t(), String.t() | nil) :: t()

The plain-text body. nil removes it.

to(builder, addresses)

@spec to(t(), String.t() | [String.t()]) :: t()

Replaces the recipients. Takes one address or a list.