SwooshCloudflare.Event (SwooshCloudflare v0.7.1)

Copy Markdown View Source

A Cloudflare Email Sending lifecycle event, as published to a Cloudflare Queue by an event subscription.

Use parse/1 on the decoded message body (a map) or on the raw JSON string:

{:ok, %SwooshCloudflare.Event{type: :bounced, recipient: "user@example.net"}} =
  SwooshCloudflare.Event.parse(json_or_map)

Use SwooshCloudflare.Queues.pull/3 to receive these events over HTTP.

Fields

  • :type - one of :delivered, :deferred, :bounced, :failed, :rejected, :complained, or :unknown for an event type this library does not know.
  • :raw_type - the type string as received (for example "cf.email.sending.message.delivered").
  • :event_id, :message_id, :sender, :recipient, :subject, :terminal - from payload (eventId, messageId, ...). :subject is absent for some events.
  • :delivery, :bounce, :failure, :rejection, :complaint - the corresponding payload sub-maps with their original string keys (for example %{"status" => "bounced", "smtpStatusCode" => "550"}), or nil when absent.
  • :metadata - the metadata map (accountId, eventSubscriptionId, eventSchemaVersion, eventTimestamp) and :source - the source map (type, zoneId, domain).
  • :timestamp - metadata.eventTimestamp as a DateTime, or nil.
  • :raw - the full decoded map, so fields this library does not model are not lost.

Correlating an event with a sent email

Events carry payload.messageId (for example "0101018f7d0c4d9a-msg-deadbeef"). The Cloudflare documentation does not say whether this value equals the message_id returned by the /send endpoint (the Swoosh :id in the delivery result), nor whether it is wrapped in angle brackets like an RFC 5322 Message-ID. This is UNVERIFIED: the REST API documentation shows no message_id in the /send response, and the documented event examples use bare values without brackets. Compare a real /send result with a real event before relying on it, and consider stripping < and > from both sides when matching.

Summary

Functions

Parses a decoded event map or a JSON string into an Event.

Types

event_type()

@type event_type() ::
  :delivered | :deferred | :bounced | :failed | :rejected | :complained

t()

@type t() :: %SwooshCloudflare.Event{
  bounce: map() | nil,
  complaint: map() | nil,
  delivery: map() | nil,
  event_id: String.t() | nil,
  failure: map() | nil,
  message_id: String.t() | nil,
  metadata: map() | nil,
  raw: map(),
  raw_type: String.t(),
  recipient: String.t() | nil,
  rejection: map() | nil,
  sender: String.t() | nil,
  source: map() | nil,
  subject: String.t() | nil,
  terminal: boolean() | nil,
  timestamp: DateTime.t() | nil,
  type: event_type() | :unknown
}

Functions

parse(json)

@spec parse(map() | binary()) :: {:ok, t()} | {:error, :invalid_json | :invalid_event}

Parses a decoded event map or a JSON string into an Event.

Returns {:error, :invalid_json} for a string that is not valid JSON and {:error, :invalid_event} for anything that does not have the event envelope (a "type" string and a "payload" map). Never raises.