Swoosh.Adapters.Cloudflare (SwooshCloudflare v0.7.1)

Copy Markdown View Source

Swoosh adapter for Cloudflare Email Service REST API.

Configuration

Put this in config/runtime.exs, since it reads environment variables. This adapter uses Req directly, so also set config :swoosh, :api_client, false in config/config.exs.

config :my_app, MyApp.Mailer,
  adapter: Swoosh.Adapters.Cloudflare,
  api_token: System.get_env("CLOUDFLARE_EMAIL_TOKEN"),
  account_id: System.get_env("CLOUDFLARE_ACCOUNT_ID")

Options

  • :base_url - overrides the Cloudflare API base URL.

  • :req_options - keyword list merged into the Req options of every request (adapter, SwooshCloudflare.send_raw/4 and SwooshCloudflare.Suppressions). Use it for proxy, Finch pool, connect_options, retry, timeouts or plug: {Req.Test, ...}. Precedence is library defaults (receive_timeout: 15_000) < :req_options < per-call options. method, url and the Authorization header are always set by the library; extra :headers are appended.

    config :my_app, MyApp.Mailer,
      adapter: Swoosh.Adapters.Cloudflare,
      api_token: System.get_env("CLOUDFLARE_EMAIL_TOKEN"),
      account_id: System.get_env("CLOUDFLARE_ACCOUNT_ID"),
      req_options: [receive_timeout: 30_000, retry: false]

Provider options

Swoosh.Email.put_provider_option/3 values are merged into the JSON payload (keys converted to strings), overriding built fields. It is an escape hatch for API fields this adapter does not map yet:

Swoosh.Email.put_provider_option(email, :some_new_field, "value")

Success response

deliver/2 returns {:ok, %{id: ..., delivered: [...], permanent_bounces: [...], queued: [...], suppressed_recipients: [...]}}.

  • id is the Cloudflare message ID, which you can trace in the Email Service activity log.
  • suppressed_recipients lists recipients dropped because they are on the suppression list (only when "Drop suppressed recipients" is enabled on the sending domain). It is [] otherwise.

Sending many emails

deliver_many/2 returns {:ok, results}, where results is a list with one {:ok, map} | {:error, term} per email, in input order.

Emails are sent concurrently, up to :max_concurrency at a time (config key, default System.schedulers_online()). Each task is given the effective receive_timeout (from :req_options, default 15_000 ms) plus 5_000 ms; a task that exceeds it is killed and its result is {:error, :timeout}. A timeout result is ambiguous: the email may have been sent anyway.

Inline images

Use type: :inline with a cid, e.g. Swoosh.Attachment.new("logo.png", type: :inline, cid: "logo"), and reference it in the HTML body with <img src="cid:logo">.

Error responses

deliver/2 returns {:error, {status, reason, body}} for most errors, with Cloudflare's response body so its message is not lost, and {:error, {429, :rate_limited, retry_after_seconds}} for rate limiting, where retry_after_seconds is an integer or nil if the header was absent. Any 2xx with a JSON body is success; a 2xx without one is {:error, {status, :invalid_response, body}}.

Header validation

Custom headers are validated before any request is made. Headers Cloudflare controls (Date, Message-ID, MIME-Version, Content-Type, Content-Transfer-Encoding, DKIM-Signature, Return-Path, Received, Feedback-ID, ARC-*, TLS-*, CFBL-*) and headers that have a dedicated API field (From, To, Cc, Bcc, Subject, Reply-To) are rejected, case-insensitively, and no request is sent:

  • {:error, {:invalid_header, header_name, :use_api_field}}
  • {:error, {:invalid_header, header_name, :not_allowed}}
  • {:error, {:invalid_header, header_name, :invalid_value}} (a nil, empty or unconvertible value)

Headers set through provider_options are validated the same way. Empty values and values that cannot be converted to a string are also :invalid_value. Non-string header values are converted with to_string/1. Cloudflare applies further rules of its own (for example only X-* and a fixed set of standard headers are accepted); those are reported by the API as usual. See SwooshCloudflare.put_list_unsubscribe/3 for one-click unsubscribe.

Limitations

  • Maximum total message size is 25 MiB, including attachments.
  • Maximum 50 recipients per email (combined to/cc/bcc).
  • Subject: maximum 998 characters.
  • Custom headers: maximum 16 KB combined.
  • Before onboarding a sending domain, you can only send to verified destination addresses. After onboarding a sending domain, you can send to any recipient.
  • The from address must be on an onboarded sending domain.
  • Transactional email only. Bulk/marketing sending is not supported.

Summary

Types

Error returned by deliver/2 and SwooshCloudflare.send_raw/4.

Successful send result.

Types

error()

@type error() ::
  {:invalid_header, String.t(), :use_api_field | :not_allowed | :invalid_value}
  | {429, :rate_limited, non_neg_integer() | nil}
  | {pos_integer(), :invalid_response, term()}
  | {pos_integer(), atom(), term()}
  | Exception.t()
  | term()

Error returned by deliver/2 and SwooshCloudflare.send_raw/4.

Besides the tuples below, transport errors (for example Req.TransportError) are passed through unchanged.

send_result()

@type send_result() :: %{
  id: String.t() | nil,
  delivered: [String.t()],
  permanent_bounces: [String.t()],
  queued: [String.t()],
  suppressed_recipients: [String.t()]
}

Successful send result.

Functions

validate_config(config)

Callback implementation for Swoosh.Adapter.validate_config/1.

validate_dependency()

Callback implementation for Swoosh.Adapter.validate_dependency/0.