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:unknownfor 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- frompayload(eventId,messageId, ...).:subjectis absent for some events.:delivery,:bounce,:failure,:rejection,:complaint- the correspondingpayloadsub-maps with their original string keys (for example%{"status" => "bounced", "smtpStatusCode" => "550"}), ornilwhen absent.:metadata- themetadatamap (accountId,eventSubscriptionId,eventSchemaVersion,eventTimestamp) and:source- thesourcemap (type,zoneId,domain).:timestamp-metadata.eventTimestampas aDateTime, ornil.: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
@type event_type() ::
:delivered | :deferred | :bounced | :failed | :rejected | :complained
@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
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.