SwooshCloudflare.Suppressions (SwooshCloudflare v0.7.1)

Copy Markdown View Source

Manage the Cloudflare Email Service suppression list.

Every function takes the same config keyword list as the mailer (:api_token, :account_id, optionally :base_url):

config = Application.get_env(:my_app, MyApp.Mailer)

{:ok, %{suppressions: list, next_cursor: cursor}} =
  SwooshCloudflare.Suppressions.list(config, reason: :hard_bounce, per_page: 100)

{:ok, suppression} = SwooshCloudflare.Suppressions.get("id", config)

{:ok, _} =
  SwooshCloudflare.Suppressions.create("bob@example.com", config,
    scope: {:sending_domain, "mail.example.com"},
    note: "asked to stop",
    expires_at: ~U[2030-01-01 00:00:00Z]
  )

{:ok, _} = SwooshCloudflare.Suppressions.update("id", config, expires_at: nil, note: "")
{:ok, _} = SwooshCloudflare.Suppressions.delete("id", config)

{:ok, _} =
  SwooshCloudflare.Suppressions.import([%{email: "a@example.com"}, [email: "b@example.com"]], config)

Suppressions are returned as decoded from Cloudflare, with string keys ("id", "email", "reason", "created_at", "expires_at", "read_only", "note", "scope").

Errors use the same shapes as the mailer: {:error, {status, reason, body}}, {:error, {429, :rate_limited, retry_after}}, or a network error term.

Summary

Functions

Creates a suppression.

Deletes a suppression by id.

Fetches a single suppression by id.

Creates up to 1000 suppressions in one request.

Lists suppressions, one page at a time.

Updates a suppression's :note and/or :expires_at.

Types

config()

@type config() :: keyword()

result()

@type result() :: {:ok, map()} | {:error, term()}

Functions

create(email, config, opts \\ [])

@spec create(String.t(), config(), keyword()) :: result()

Creates a suppression.

Options: :scope (:account or {:sending_domain, "domain"}; defaults to account), :note, and :expires_at (a DateTime or an ISO8601 string; omit for a permanent suppression).

delete(id, config)

@spec delete(String.t(), config()) :: result()

Deletes a suppression by id.

get(id, config)

@spec get(String.t(), config()) :: result()

Fetches a single suppression by id.

import(items, config)

@spec import([map() | keyword()], config()) :: result()

Creates up to 1000 suppressions in one request.

Each item is a map or keyword list with the same keys as create/3 (:email required, :scope, :note, :expires_at). Raises ArgumentError for more than 1000 items.

list(config, opts \\ [])

@spec list(config(), keyword()) ::
  {:ok, %{suppressions: [map()], next_cursor: String.t() | nil}}
  | {:error, term()}

Lists suppressions, one page at a time.

Options (all optional, only given ones are sent): :cursor, :email, :per_page (max 1000), :reason (:manual, :complaint, :hard_bounce, :soft_bounce, :policy), :scope_type (:account, :sending_domain), :scope_value (requires scope_type: :sending_domain) and :search.

Returns {:ok, %{suppressions: [map], next_cursor: String.t() | nil}}. Pass next_cursor as :cursor to fetch the next page.

update(id, config, opts)

@spec update(String.t(), config(), keyword()) :: result()

Updates a suppression's :note and/or :expires_at.

An omitted option leaves the field unchanged. expires_at: nil makes the suppression permanent and note: "" clears the note. The scope cannot be changed.