cloudflare-email 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -2
  3. data/README.md +63 -379
  4. data/SECURITY.md +1 -9
  5. data/docs/activerecord-tenanted.md +0 -11
  6. data/docs/custom-ingress.md +1 -15
  7. data/docs/delivery-events.md +2 -5
  8. data/docs/features.md +49 -123
  9. data/docs/getting-started.md +90 -172
  10. data/docs/mailboxes.md +24 -60
  11. data/docs/management-engine.md +1 -3
  12. data/docs/outbox.md +5 -6
  13. data/docs/reference.md +190 -0
  14. data/docs/routing-diagnostics.md +0 -2
  15. data/docs/thread-correlation.md +2 -4
  16. data/docs/troubleshooting.md +4 -4
  17. data/lib/cloudflare/email/client.rb +23 -5
  18. data/lib/cloudflare/email/version.rb +1 -1
  19. data/templates/deploy-to-cloudflare/README.md +8 -10
  20. data/templates/deploy-to-cloudflare/docs/domain-setup.md +86 -166
  21. data/templates/worker/README.md +6 -8
  22. data/templates/worker/docs/domain-setup.md +86 -166
  23. data/templates/worker/docs/durable-inbound.md +4 -16
  24. metadata +2 -17
  25. data/docs/architecture.md +0 -105
  26. data/docs/tenant-mailboxes-plan.md +0 -149
  27. data/docs/upgrading-0.2.md +0 -101
  28. data/docs/upgrading-0.3.md +0 -55
  29. data/docs/verification/2026-09-10-followup.md +0 -49
  30. data/docs/verification/2026-09-10-install-upgrade.md +0 -86
  31. data/docs/verification/2026-09-10-live.md +0 -102
  32. data/docs/verification/2026-09-10.md +0 -175
  33. data/docs/verification/2026-09-11-custom-ingress.md +0 -49
  34. data/docs/verification/2026-09-11-extraction.md +0 -32
  35. data/docs/verification/2026-09-11-outbound-ledger.md +0 -60
  36. data/docs/verification/2026-09-11-security.md +0 -76
  37. data/docs/verification/2026-09-11-tenant-mailboxes.md +0 -75
  38. data/docs/verification/2026-09-12-destination-context.md +0 -46
  39. data/docs/verification/2026-09-13-action-mailbox-core.md +0 -96
  40. data/docs/verification/2026-09-13-durable-inbound-and-routing.md +0 -71
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c65b29d22af39c25cb2affb776984bc9a9086bcad75c7388c2017692f61eb9ef
4
- data.tar.gz: 2c0d1f15f4159e780257260af62cca8d414c7cc00d7c1c8b2677ac243cf81b04
3
+ metadata.gz: b53307e62ade70f522e0fcb5705e11552e9718914d02bea8754a11653b15349a
4
+ data.tar.gz: 692c09a19fcb4ccd9295ccb692a19bf02aba3176b107c8500e5450b100ce1df3
5
5
  SHA512:
6
- metadata.gz: 4b76b7c08c99da504f7c90bbfb79f6f3130345e128c0fad9988a1449270d79019ffa5af8fb62a0759ad881c56019ffe6d9f6b967d13136e988b9e98ef6d7043e
7
- data.tar.gz: d3f2d2573c6d41140ceff2395c4aa84194eb10ad2c56379df133f1fa1687424b009513800e4c67d1077584750146c2f5b9e0dfedfb53d2b9a351c5f207f80636
6
+ metadata.gz: 43da0e85cde52970e696d5a2dfd74f03c0f39f5ea7d66e33cd28ec2bb58e5872c57db7f5dbc010ab85ad43312de9c9abcf2eed31a65e01f78fed8b6de164483b
7
+ data.tar.gz: a38902643ce082d656a01b7d91a34e70f95f15d66de6fd06e1fe7502461ae6d155a64edcea780155dd9b00b055635a3b10d5a0e9d8cbf2d077e1a7782742b193
data/CHANGELOG.md CHANGED
@@ -1,9 +1,38 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.1 — 2026-10-06
4
+
5
+ - Report the provider's own reason when a refused request returns a body that
6
+ is not a Cloudflare JSON object. Cloudflare Queues throttles a pull with
7
+ `429` and the bare text `Queue is overloaded. Please back off.`; that raised
8
+ `RateLimitError: invalid JSON API response; request outcome is unknown`, which
9
+ dropped the reason and called a known refusal unknown. It now raises
10
+ `RateLimitError: HTTP 429: Queue is overloaded. Please back off.` The text is
11
+ collapsed to one line and capped at 200 characters; an HTML error page is
12
+ still never echoed. A `5xx` keeps "request outcome is unknown" because it may
13
+ have failed after taking effect, and an unreadable `2xx` still fails closed.
14
+ Error classes and retry behaviour are unchanged.
15
+ - Honour `Retry-After` on a `429` whose body is a bare JSON string. The parsed
16
+ string was not a Hash, so the header was never stashed and the retry slept
17
+ the default backoff instead.
18
+ - Describe an unreadable provider response as an unknown *request* outcome rather
19
+ than an unknown *delivery* outcome. `Client#request` serves every caller, so a
20
+ queue pull that timed out reported that a delivery outcome was unknown when no
21
+ message was in flight — sending operators to look for an email that never
22
+ existed.
23
+
24
+ This message reaches **every** caller of `Client#request`, sending included:
25
+ nothing wraps or rescues it in `DeliveryMethod` or `Outbox.deliver`, and
26
+ `SendJob`'s "delivery outcome is unknown; reconcile before retrying" is a
27
+ separate condition (retrying a delivery already in the `unknown` state), not a
28
+ wrapper around this error. A send caller now reads "request outcome is unknown",
29
+ which remains accurate for a send — the request *is* the send — and stops the
30
+ same string from asserting a delivery on the paths that never had one.
31
+
3
32
  ## 0.4.0 — 2026-09-14
4
33
 
5
34
  Released alongside Mailbox Kit 0.1.0. Cloudflare installs the core automatically.
6
- Existing installations should follow the [core upgrade guide](mailbox-kit/docs/upgrading.md).
35
+ Existing installations should follow the [core upgrade guide](maintainer/upgrading-mailbox-kit.md).
7
36
 
8
37
  - Allow receiving domains without a sending account. The upgrade generator
9
38
  preserves existing directory records while making account ownership optional.
@@ -35,7 +64,7 @@ Existing installations should follow the [core upgrade guide](mailbox-kit/docs/u
35
64
 
36
65
  ## 0.3.0 — 2026-09-13
37
66
 
38
- See [upgrading from 0.2](docs/upgrading-0.3.md) before deploying the updated Worker.
67
+ See [upgrading from 0.2](maintainer/upgrading-0.3.md) before deploying the updated Worker.
39
68
 
40
69
  - Add a standalone Deploy to Cloudflare template with R2/Queue provisioning,
41
70
  secret prompts, scheduled recovery, and logs. CI checks runtime/configuration
data/README.md CHANGED
@@ -1,177 +1,78 @@
1
- # cloudflare-email
1
+ # Cloudflare Email
2
2
 
3
- Ruby client for [Cloudflare Email Service](https://developers.cloudflare.com/email-service/), with ActionMailer, authenticated ActionMailbox ingress, a forwarding Worker, and optional durable Rails sending and delivery-event tracking.
3
+ Send and receive email in Ruby and Rails through [Cloudflare Email Service](https://developers.cloudflare.com/email-service/).
4
4
 
5
- Version **0.4.0**, with **Mailbox Kit 0.1.0**. Ruby 3.2+, Rails 7.2–8.1; Ruby 4.0 is tested with Rails 8.1. Supported test floors are Rails 7.2.3.2, 8.0.5.1, and 8.1.3.1. Prefer a maintained Ruby/Rails release for new applications. Rails and database dependencies remain optional for the plain Ruby client. Node is optional: Worker deployment also works through the included Ruby deployer. See [security guidance](SECURITY.md) for deployment responsibilities.
5
+ Use your existing Rails mailers, receive messages through Action Mailbox, and add
6
+ persistent inboxes with [Mailbox Kit](mailbox-kit/README.md). SQLite is supported.
7
+ Database tenancy and the management UI are optional.
6
8
 
7
- [![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cole-robertson/cloudflare-email/tree/main/templates/deploy-to-cloudflare)
8
-
9
- Deploy the inbound Worker with guided R2/Queue provisioning and secret setup. [Prepare Rails and connect your email address](templates/deploy-to-cloudflare/README.md).
10
-
11
- ## Start here
12
-
13
- **Starting in 0.4.0:** provider-neutral mailbox functionality lives in
14
- the sibling [Mailbox Kit](mailbox-kit/README.md) gem. Existing Cloudflare setup
15
- continues to work; the core requires no Cloudflare account. Installing
16
- `cloudflare-email` also installs `mailbox-kit` 0.1.x.
17
- Existing mailbox installations should use the [core upgrade guide](mailbox-kit/docs/upgrading.md)
18
- to add the inbound lookup index without recreating their tables.
19
-
20
- **Sending email?** Start with the [step-by-step Rails guide](docs/getting-started.md).
21
- **Building a mailbox?** Follow the same guide through receiving, the SQLite-compatible outbox, and delivery tracking.
22
- **Using plain Ruby?** Jump to [Plain Ruby](#plain-ruby); Rails and a database are optional.
23
-
24
- | Guide | What you will learn |
25
- | --- | --- |
26
- | [Features at a glance](docs/features.md) | Everything the gem handles, and what your app supplies |
27
- | [Getting started](docs/getting-started.md) | Install, send your first email, receive replies, and save reliable send operations |
28
- | [Cloudflare domain setup](templates/worker/docs/domain-setup.md) | Configure once for many mailboxes or dynamic organization subdomains, with a Rebulk example and DNS checks |
29
- | [Hello-world Rails template](https://github.com/cole-robertson/cloudflare-email-rails-starter) | Clone a minimal SQLite mailbox app using the published gem and its management UI |
30
- | [Troubleshooting](docs/troubleshooting.md) | What to check when mail or delivery updates do not arrive |
31
- | [Managed mailboxes](docs/mailboxes.md) | Create inboxes and aliases, read/archive mail, and send from a mailbox |
32
- | [Management engine](docs/management-engine.md) | Mount an optional server-rendered mailbox UI using your app's authentication |
33
- | [Custom ingress](docs/custom-ingress.md) | Reuse authentication, signed metadata, and tenant routing in your existing ingestion pipeline |
34
- | [Routing diagnostics](docs/routing-diagnostics.md) | Inspect exact-domain DNS and Worker routes without changing infrastructure |
35
- | [Reusable Worker pipeline](templates/worker/README.md#reuse-the-transport-in-an-existing-worker) | Keep custom backend and archive policies while sharing bounded email forwarding |
36
- | [Durable inbound delivery](templates/worker/README.md#durable-inbound-delivery) | Store incoming mail in R2 and recover Rails outages with queued retries and scheduled recovery |
37
- | [Routing delivery confirmation](docs/routing-deliveries.md) | Confirm qualifying normal-address deliveries using authenticated Routing analytics |
38
- | [SQLite tenant databases](docs/activerecord-tenanted.md) | Give each organization its own SQLite database with `activerecord-tenanted` |
39
- | [Durable outbox](docs/outbox.md) | Detailed setup, callbacks, retries, and recovery |
40
- | [Delivery events](docs/delivery-events.md) | Cloudflare Queue setup and recipient status tracking |
41
- | [Upgrading to 0.4](mailbox-kit/docs/upgrading.md) | Core extraction and data-preserving migrations for existing mailboxes |
42
- | [Upgrading from 0.2](docs/upgrading-0.3.md) | Additional changes before deploying the durable Worker |
43
-
44
- The sections below are the configuration and API reference.
45
-
46
- ## Install and send from Rails
47
-
48
- Add version 0.4 to your Gemfile. Existing mailbox users should follow the [core upgrade guide](mailbox-kit/docs/upgrading.md). When upgrading from 0.2, also follow the [durable Worker upgrade guide](docs/upgrading-0.3.md):
9
+ ## Install
49
10
 
50
11
  ```ruby
51
12
  gem "cloudflare-email", "~> 0.4.0"
52
13
  ```
53
14
 
15
+ Mailbox Kit installs automatically. If you use another email provider, you can
16
+ [use Mailbox Kit on its own](mailbox-kit/README.md).
17
+
18
+ Requires Ruby 3.2+; Rails integrations support Rails 7.2–8.1.
19
+ Use a current patch release. Plain Ruby sending does not require Rails or a database.
20
+
21
+ ## Send from Rails
22
+
54
23
  ```sh
55
24
  bundle install
56
25
  bin/rails generate cloudflare:email:install --no-inbound
57
26
  ```
58
27
 
59
- Add Rails credentials (encrypted, per environment) or environment variables:
60
-
61
- ```yaml
62
- cloudflare:
63
- account_id: your-account-id
64
- api_token: your-email-sending-token
65
- ```
28
+ [Verify a sending domain in Cloudflare](https://developers.cloudflare.com/email-service/configuration/domains/)
29
+ and add your account ID and sending token:
66
30
 
67
31
  ```sh
68
32
  export CLOUDFLARE_ACCOUNT_ID=your-account-id
69
33
  export CLOUDFLARE_API_TOKEN=your-email-sending-token
70
34
  ```
71
35
 
72
- The generated initializer uses `Cloudflare::Email::Credentials`: nonempty Rails credentials take precedence, then `CLOUDFLARE_*` environment variables. For an existing 0.1.0 installation, update the initializer manually; see [upgrading](docs/upgrading-0.2.md).
73
-
74
- Onboard a sending domain under **Compute → Email Service → Email Sending**. Use a dedicated sending subdomain if the apex already uses another mail provider. Cloudflare's onboarding adds the required bounce MX, SPF, DKIM, and DMARC records for that sending domain. Follow the current [domain configuration](https://developers.cloudflare.com/email-service/configuration/domains/) instructions; do not replace an existing apex SPF record or publish a second one.
75
-
76
- Create a token with permission to send for your account, and verify the domain in the dashboard before testing:
77
-
78
- ```sh
79
- bin/rails cloudflare:email:doctor
80
- FROM=hello@mail.example.com TO=you@example.net bin/rails cloudflare:email:send_test
81
- ```
82
-
83
- `doctor` checks configuration and available read access. It does not send mail or prove deliverability. `send_test` requires both `FROM` and `TO`.
84
-
85
- Standard ActionMailer works:
36
+ Your regular mailers can now send through Cloudflare:
86
37
 
87
38
  ```ruby
88
- class WelcomeMailer < ApplicationMailer
89
- def welcome(user)
90
- mail(from: "hello@mail.example.com", to: user.email, subject: "Welcome") do |format|
91
- format.text { render plain: "Hello!" }
39
+ class HelloMailer < ApplicationMailer
40
+ def hello(address)
41
+ mail(from: "hello@mail.example.com", to: address, subject: "Hello") do |format|
42
+ format.text { render plain: "Hello from Rails!" }
92
43
  end
93
44
  end
94
45
  end
95
46
 
96
- WelcomeMailer.welcome(user).deliver_later
97
- ```
98
-
99
- Multipart, attachments, cc/bcc, and threading headers are serialized through `send_raw`. Cloudflare still controls final delivery and header acceptance.
100
-
101
- ## Managed mailboxes and optional tenancy
102
-
103
- **Multi-tenancy is off by default.** Installing the gem does not create tenant
104
- databases, install a tenancy library, or change your application's database routing.
105
-
106
- | Setup | What you explicitly enable |
107
- | --- | --- |
108
- | Plain Ruby sending | Nothing extra; no Rails/database required |
109
- | Managed mailboxes in one database | Run the mailbox generator and load the optional module |
110
- | Separate tenant databases | Configure `Tenancy.configure(...)` with your application's base class and switching adapter before loading models |
111
-
112
- The mailbox API uses a tenant key to group and scope records even in one database.
113
- That key alone does not enable database switching. `activerecord-tenanted` is an
114
- optional application dependency, not a runtime dependency of this gem.
115
-
116
- Create mailboxes in code with the optional mailbox module. It includes named
117
- mailboxes, aliases, ownership references, read/archive state, retained raw mail,
118
- mailbox-authorized sends, and tenant-aware delivery-event recovery:
119
-
120
- ```sh
121
- bin/rails generate cloudflare:email:mailboxes
122
- bin/rails db:migrate
47
+ HelloMailer.hello("you@example.net").deliver_now
123
48
  ```
124
49
 
125
- After registering and verifying a receiving domain:
50
+ HTML templates, attachments, multipart messages, cc/bcc, and reply headers work
51
+ through Action Mailer. Follow the [Rails setup guide](docs/getting-started.md)
52
+ for receiving and delivery tracking.
126
53
 
127
- ```ruby
128
- Cloudflare::Email::Mailboxes.for_tenant("organization-123") do |inboxes|
129
- mailbox = inboxes.create(name: "Support", address: "support@acme.example.com",
130
- owner_ref: "team:42")
131
- # Provision and activate its address route before receiving or sending mail.
132
- end
133
- ```
54
+ ## Receive email
134
55
 
135
- Use one database or an application-provided tenant base class. The optional
136
- `activerecord-tenanted` integration is tested with separate SQLite databases,
137
- including jobs and overlapping record IDs. Your app still authorizes users and
138
- provides the UI. Start with [the mailbox guide](docs/mailboxes.md), and configure
139
- [tenant connections](docs/activerecord-tenanted.md) before loading models when
140
- using separate databases. Plain Ruby users do not load this module.
56
+ The included Worker stores incoming email at Cloudflare and retries delivery
57
+ while Rails is unavailable. Rails verifies each request and stores the original
58
+ email through Action Mailbox.
141
59
 
142
- ## Durable mailbox delivery from Rails
60
+ [![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cole-robertson/cloudflare-email/tree/main/templates/deploy-to-cloudflare)
143
61
 
144
- For a mailbox application, use the optional outbox instead of recreating send
145
- claims, attempt records, recipient outcomes, and event correlation:
62
+ [Prepare Rails and deploy the Worker](templates/deploy-to-cloudflare/README.md),
63
+ then connect your receiving address in Cloudflare Email Routing.
146
64
 
147
- ```sh
148
- bin/rails generate cloudflare:email:tracking
149
- bin/rails generate cloudflare:email:outbox
150
- bin/rails db:migrate
151
- ```
65
+ ## Build an inbox
152
66
 
153
- ```ruby
154
- account = Cloudflare::Email::Credentials.account_id
155
- operation = Cloudflare::Email::ActiveRecord::Outbox.prepare_mail(
156
- account_id: account,
157
- operation_key: "reply:#{draft.id}:revision:#{draft.revision}",
158
- mail: ReplyMailer.reply(draft).message,
159
- )
160
- # Enqueue after the transaction that prepared the operation commits.
161
- Cloudflare::Email::SendJob.perform_later(account, operation.operation_key)
162
- ```
67
+ The [mailbox guide](docs/mailboxes.md) walks through creating inboxes and aliases,
68
+ receiving mail, marking it read, archiving it, and sending replies.
163
69
 
164
- The job sends the stored MIME once per operation claim. Accepted repeats cannot
165
- resend; uncertain sends remain blocked for audited reconciliation. Each recipient
166
- has separate acceptance evidence and lifecycle status. `DeliveryEvents` persists,
167
- deduplicates, correlates, orders, and replays queue events, with transactional
168
- callbacks for your product records. The reference Rails inbox uses these same
169
- tables and APIs for compose, replies, approved AI drafts, and recovery.
70
+ Mount the optional [management UI](docs/management-engine.md) to manage mailboxes
71
+ and read messages using your app's login and permissions. It is server-rendered
72
+ and needs no frontend build.
170
73
 
171
- `ReplyMailer`, `draft`, and its revision are application examples. Authorize the
172
- send before preparation and use a durable job backend. See the [complete outbox
173
- setup and recovery guide](docs/outbox.md) and [gem/inbox architecture](docs/architecture.md).
174
- The adapter is opt-in; the plain Ruby client does not load Rails or ActiveRecord.
74
+ For a runnable SQLite example, clone the
75
+ [hello-world Rails app](https://github.com/cole-robertson/cloudflare-email-rails-starter).
175
76
 
176
77
  ## Plain Ruby
177
78
 
@@ -184,250 +85,33 @@ client = Cloudflare::Email::Client.new(
184
85
  )
185
86
 
186
87
  response = client.send(
187
- from: { address: "hello@mail.example.com", name: "Example" },
88
+ from: "hello@mail.example.com",
188
89
  to: "you@example.net",
189
90
  subject: "Hello",
190
- text: "Plain body",
191
- html: "<p>HTML body</p>",
192
- reply_to: "support@in.example.com",
193
- )
194
- response.message_id # Provider ID when returned; nil on older responses
195
- response.delivered # Immediately delivered recipients
196
- response.queued # Recipients queued for later delivery
197
- response.permanent_bounces # Recipients that permanently bounced
198
- response.suppressed_recipients # Recipients dropped by suppression policy
199
- ```
200
-
201
- `to` may be omitted for cc-only or bcc-only mail. Addresses accept strings or `{ address:, name: }` hashes. Attachments use Cloudflare's `content` (base64), `filename`, `type`, `disposition`, and optional `content_id` fields. For full MIME control, use `client.send_raw(from:, recipients:, mime_message:)`.
202
-
203
- An HTTP success does not mean every recipient received the message. Inspect the recipient outcome arrays and use [delivery events](docs/delivery-events.md) for later results. The current API reference includes message IDs and suppressed recipients; older responses remain supported.
204
-
205
- Cloudflare currently limits ordinary sends to 50 recipients and 5 MiB including attachments; verified destination addresses have a 25 MiB allowance. These limits are enforced by Cloudflare. See [limits](https://developers.cloudflare.com/email-service/platform/limits/) and the [header allowlist](https://developers.cloudflare.com/email-service/reference/headers/).
206
-
207
- ## Receive through ActionMailbox
208
-
209
- Cloudflare Email Routing invokes an Email Worker. The bundled Worker forwards unchanged raw MIME to Rails with a versioned HMAC-SHA256 signature covering the timestamp, SMTP envelope, and body. Rails verifies the signature and a five-minute timestamp window before storing the message in ActionMailbox.
210
-
211
- ```sh
212
- bin/rails generate cloudflare:email:install
213
- ```
214
-
215
- The interactive installer offers ActionMailbox installation/migrations and a default `MainMailbox`, copies the Worker, and configures ingress in development and production. Add `--all-envs` to include test; configure custom staging environments explicitly. Send-only apps do not need ActionMailbox.
216
-
217
- Add the generated ingress secret and an optional management token:
218
-
219
- ```yaml
220
- cloudflare:
221
- account_id: your-account-id
222
- api_token: your-runtime-send-token
223
- management_token: your-deployment-token
224
- ingress_secret: the-generated-random-secret
225
- ```
226
-
227
- Equivalent environment names are `CLOUDFLARE_MANAGEMENT_TOKEN` and `CLOUDFLARE_INGRESS_SECRET`. Keep management credentials in the deployment environment rather than the running application. Credentials stored in the application's Rails credentials are accessible to that application; this gem is not a secret-isolation boundary.
228
-
229
- 1. Add the receiving subdomain, for example `in.example.com`, in **Email Routing → apex domain → Settings → Subdomains**. This is a separate onboarding step from sending.
230
- 2. Deploy the environment's Worker and create its route:
231
-
232
- First create the private R2 bucket and Queue, then deploy the generated Wrangler
233
- configuration using the [durable setup guide](templates/worker/docs/durable-inbound.md).
234
- The Ruby task below updates that provisioned Worker while preserving its bindings.
235
- It refuses a missing bucket/queue binding or recovery schedule before changing code.
236
-
237
- ```sh
238
- RAILS_ENV=production bin/rails cloudflare:email:deploy_worker URL=https://app.example.com/rails/action_mailbox/cloudflare/inbound_emails
239
- RAILS_ENV=production bin/rails cloudflare:email:provision_route ADDRESS=support@in.example.com
240
- ```
241
-
242
- Subdomain provisioning checks configured DNS before creating a rule. It never enables the parent apex on behalf of a subdomain. A missing setup or permission fails with instructions. DNS records alone do not prove propagation or live routing; send a real test afterward.
243
-
244
- For a zone apex, provisioning may enable Email Routing and its DNS records. Only use this when Cloudflare should handle mail for that apex. `provision_catchall DOMAIN=example.com` changes the **zone-wide** catch-all; a subdomain that resolves to a parent zone is rejected. See [subdomain onboarding](https://developers.cloudflare.com/email-service/configuration/subdomains/).
245
-
246
- Replace the scaffolded mailbox's `process` with your application logic. For tenant or mailbox selection, use the authenticated SMTP recipient rather than the sender-controlled MIME `To`/`Cc` headers:
247
-
248
- ```ruby
249
- class ApplicationMailbox < ActionMailbox::Base
250
- routing ->(inbound) { Cloudflare::Email::Envelope.for(inbound)&.fetch("to")&.match?(/\Asupport@/i) } => :support
251
- end
252
- ```
253
-
254
- `Cloudflare::Email::Envelope.for(inbound_email)` returns a string-keyed `{"from" => "sender@example.com", "to" => "support@example.com"}` hash, or `nil` when the record has no authenticated envelope (for example, another ingress). The metadata is stored on the raw-email blob before routing jobs enqueue. It does not modify the MIME source. Envelope sender information records the SMTP reverse path; it does not authenticate the human sender. An empty `from` is valid for bounces.
255
-
256
- The bundled Worker uses v2 signatures by default; missing or v1 signatures are rejected. The custom-ingress API also supports opt-in v3 signatures carrying authenticated Worker metadata. See [custom ingestion](docs/custom-ingress.md) and upgrade the Rails receiver before enabling v3 in a custom Worker. When upgrading from v1, coordinate Rails and Worker deployment while ingress is paused. Both envelope versions require ASCII dot-atom addresses, at most 254 bytes with a 64-byte local part. Quoted local parts, address literals, and internationalized addresses are not supported by this envelope format.
257
-
258
- Successful ingress storage returns HTTP 200; duplicate storage returns 200 too. The timestamp window limits request age, but is not a one-time replay ledger. Version 2 deduplication includes the exact SMTP recipient, so identical MIME delivered to separate To/Cc/Bcc recipients creates separate inbound records while a retry for the same recipient creates none.
259
-
260
- The Worker has a 15-second Rails request timeout and rejects redirects. Its default [durable inbound path](templates/worker/README.md#durable-inbound-delivery) saves messages in R2 before acceptance, then retries Rails handoffs through Queues and scheduled recovery. Provision the resources before deploying. `INBOUND_DELIVERY_MODE=direct` selects the single-attempt fallback, which can reject mail during an outage; existing retained mail keeps recovering. Rails storage acceptance does not guarantee later mailbox-job success. Monitor Rails jobs and Cloudflare Worker logs.
261
-
262
- ### Local development and deployment
263
-
264
- For a simple development tunnel, use the explicit direct fallback. Start Rails, then:
265
-
266
- ```sh
267
- INBOUND_DELIVERY_MODE=direct bin/rails cloudflare:email:deploy_worker
268
- bin/rails cloudflare:email:dev
269
- ```
270
-
271
- The dev task requires `cloudflared`, refuses environments other than development, and updates the existing development Worker's URL to a temporary tunnel. It forces a dedicated origin Host; development middleware permits only POSTs to the email ingress on that Host. Restart Rails after upgrading: the task checks that the guard is running before opening the tunnel. Configure a separate development receiving address and route. Stopping the tunnel leaves that URL in the development Worker until the next update. A Worker deployed without a URL rejects mail until the tunnel sets it.
272
-
273
- For a custom installer `--worker-dir`, pass `SCRIPT=custom-directory/src/index.js` to the Ruby `deploy_worker` task. The installer prints the corresponding command.
274
-
275
- Ruby deployment and Wrangler use matching names: `cloudflare-email-ingress-development`, `-staging`, and `-production`. Initial durable infrastructure deployment uses Wrangler and requires Node 22.12+ (or a supported newer version):
276
-
277
- ```sh
278
- cd cloudflare-worker
279
- npm ci
280
- npx wrangler secret put INGRESS_SECRET --env production
281
- npx wrangler secret put RAILS_INGRESS_URL --env production
282
- npm run deploy -- --env production
283
- ```
284
-
285
- See the [Worker README](templates/worker/README.md). Existing deployments/templates are not automatically migrated; verify routing before switching names.
286
-
287
- Rotate the shared ingress secret in Rails and the corresponding Worker during a coordinated deployment. This version has no overlapping-key rotation window; requests can fail while secrets differ.
288
-
289
- ## Outbound delivery events
290
-
291
- The new `DeliveryEvent` and `EventConsumer` APIs consume Cloudflare Email Sending lifecycle events through an HTTP pull queue: delivered, deferred, bounced, failed, rejected, and complained.
292
-
293
- ```ruby
294
- consumer = Cloudflare::Email::EventConsumer.new(
295
- account_id: ENV.fetch("CLOUDFLARE_ACCOUNT_ID"),
296
- api_token: ENV.fetch("CLOUDFLARE_QUEUES_TOKEN"),
297
- queue_id: ENV.fetch("CLOUDFLARE_EVENT_QUEUE_ID"),
298
- domains: ["mail.example.com"],
91
+ text: "Hello from Ruby!",
299
92
  )
300
- consumer.poll do |event|
301
- DeliveryEventProcessor.call(event) # Your durable, idempotent application handler
302
- end
303
- ```
304
-
305
- Each event is acknowledged only after the handler returns normally. Configure a dedicated queue, subscription, retry policy, and dead-letter queue first. See the complete [Rails and Ruby delivery-event setup](docs/delivery-events.md).
306
-
307
- Rails applications can run `bin/rails generate cloudflare:email:tracking` and
308
- `bin/rails db:migrate` to install optional durable event receipts. The adapter
309
- commits before ACK, deduplicates account/event IDs, retains unmatched events,
310
- and replays them with transactional application handlers. It requires explicit
311
- opt-in; the plain Ruby client stays independent of ActiveRecord.
312
-
313
- Shared helpers include `Response#accepted?` (any provider acceptance, not final
314
- delivery) and `DeliveryEvent#supersedes?(occurred_at:, terminal:)` for ordering
315
- matched recipient events. See the [architecture guide](docs/architecture.md) for
316
- what the gem provides and what belongs in the inbox product.
317
-
318
- Applications using the outbox can use `DeliveryEvents.record(event)` and
319
- `DeliveryEvents.replay(account_id:, message_id:)` for built-in correlation and
320
- recipient-state projection. Applications with their own delivery schema can
321
- continue to use the lower-level `EventInbox` with an explicit handler.
322
-
323
- ## Thread correlation
324
-
325
- Prefer storing the provider's returned `message_id` with your conversation and correlating inbound `In-Reply-To` / `References` against that record. Correlation does not authenticate the sender or authorize an action.
326
-
327
- Use `Cloudflare::Email::MessageId.normalize(value)` to trim whitespace and one enclosing angle-bracket pair without changing the ID's case. The old `SecureMessageId` helper has been removed.
328
-
329
- Cloudflare's current [header documentation](https://developers.cloudflare.com/email-service/reference/headers/) describes Message-ID as platform-controlled. **The September 10 live test confirmed that Cloudflare replaced custom signed IDs**, including raw-MIME and ActionMailer sends. Store the provider ID for Cloudflare reply correlation. See [thread correlation](docs/thread-correlation.md) and the [live evidence](docs/verification/2026-09-10-live.md).
330
-
331
- ## Retry and configuration
332
-
333
- `Client.new` options are also accepted by `config.action_mailer.cloudflare_settings`:
334
-
335
- | Option | Default |
336
- |---|---|
337
- | `account_id`, `api_token` | Required |
338
- | `base_url` | `https://api.cloudflare.com/client/v4` |
339
- | `timeout` | 30 seconds for open/read/write |
340
- | `total_timeout` | Defaults to `timeout`; bounds one complete HTTP attempt, including headers and streamed body |
341
- | `max_response_bytes` | 1 MiB for `Client`; 32 MiB for `EventConsumer` queue batches |
342
- | `retries` | 3 additional attempts |
343
- | `initial_backoff` | 0.5 seconds, doubling |
344
- | `max_retry_after` | 60 seconds |
345
- | `retry_ambiguous` | `false` |
346
- | `logger` | `nil`, optional `warn` logger |
347
-
348
- By default, only 429 responses and pre-send connection failures retry. Numeric and HTTP-date `Retry-After` values are honored up to the cap. Read/write timeouts, connection resets, and 5xx responses may occur after acceptance; they raise without automatically resending. Setting `retry_ambiguous: true` restores retries for those failures and can send duplicates.
349
-
350
- Responses are streamed with a byte limit, including decompressed content and
351
- error responses. A total deadline or response-size failure after transmission
352
- does not prove a send failed: the outbox preserves the ambiguous claim and blocks
353
- automatic resend. Net::HTTP's implicit retries are disabled. Explicit retries
354
- and backoff can make a logical call longer than `total_timeout`; queue handlers,
355
- database transactions and a complete polling run have separate runtime budgets.
356
- An uncertain queue acknowledgement permits receipt replay, never email resending.
357
-
358
- No idempotency key is sent. Reusing Message-ID does not guarantee deduplication or exactly-once delivery. Account for ActiveJob's retry policy too: retrying the whole mailer job can resend even when this client's retries are disabled.
359
-
360
- Errors inherit from `Cloudflare::Email::Error`: `ConfigurationError`, `AuthenticationError`, `ValidationError`, `RateLimitError`, `ServerError`, and `NetworkError`. API errors expose `status` and parsed `response`.
361
-
362
- ## Observability and permissions
363
-
364
- Notifications: `cloudflare_email.send` / `send_raw` include `account_id`, `path`, `status`, `message_id`, and all four recipient outcome arrays. `cloudflare_email.ingress` includes `bytes`, `result` (`ok`, `duplicate`, `bad_signature`, `stale`, `too_large`), and the stored `message_id` when available. `cloudflare_email.delivery_event` wraps handler execution with `event_id`, `message_id`, and lifecycle `status`; it does not report queue acknowledgement completion. Instrumentation errors include ActiveSupport's exception metadata.
365
-
366
- | Task | Purpose / credentials |
367
- |---|---|
368
- | `doctor` | Read diagnostics with runtime token; limited read permissions are reported |
369
- | `send_test FROM=... TO=...` | Send a real message using runtime send permission |
370
- | `deploy_worker URL=https://...` | Management token: Workers Scripts Edit |
371
- | `provision_route ADDRESS=...` | Management token: Zone Read, Email Routing Rules Edit; DNS Read for subdomain checks; routing-settings write permission for apex enablement |
372
- | `provision_catchall DOMAIN=...` | Same routing management permissions; changes the zone-wide catch-all |
373
- | `dev` | Management token: Workers Scripts Edit; development only |
374
- | `consume_events` | Separate Queues Read/Write token, queue ID, configured handler |
375
- | `deliver OPERATION_KEY=...` | Dispatch a saved outbox operation using sending credentials |
376
- | `replay_events [MESSAGE_ID=...]` | Replay durable receipts for the configured account |
377
- | `pending_deliveries` | List prepared or uncertain outbox operations for operator review |
378
-
379
- The optional Rails adapter emits `cloudflare_email.outbox_prepare`,
380
- `cloudflare_email.outbox_send`, and `cloudflare_email.outbox_reconcile` notifications
381
- with operation identity and resulting state. These do not contain MIME or API
382
- tokens. A notification is method instrumentation; an enclosing application
383
- transaction may still roll back. Inspect the durable ledger for authoritative state.
384
-
385
- Management tasks fall back to the runtime token if `management_token` is unset. Restrict scopes and accounts to the operations you need. Event consumers use a separate `queues_token` and do not fall back to a send token.
386
-
387
- ## SMTP alternative
388
-
389
- Cloudflare also supports authenticated SMTP. Existing Rails SMTP applications can use it without this gem's delivery method:
390
-
391
- ```ruby
392
- config.action_mailer.delivery_method = :smtp
393
- config.action_mailer.smtp_settings = {
394
- address: "smtp.mx.cloudflare.net",
395
- port: 465,
396
- ssl: true,
397
- authentication: :plain,
398
- user_name: "api_token",
399
- password: ENV.fetch("CLOUDFLARE_SMTP_TOKEN"),
400
- }
401
- ```
402
-
403
- Cloudflare documents Email Sending Edit permission for SMTP, implicit TLS on port 465, and no outbound STARTTLS on 587. See [SMTP documentation](https://developers.cloudflare.com/email-service/api/send-emails/smtp/). Remove the generated `:cloudflare` initializer override if switching to SMTP.
404
-
405
- ## Development and verification
406
-
407
- ```sh
408
- bundle install
409
- bundle exec rake test
410
- bundle exec ruby script/verify_package.rb
411
- BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle install
412
- BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle exec rake test
413
- cd templates/worker
414
- npm ci
415
- npm test
416
- npm run check
417
- npm audit
418
- ```
419
-
420
- Tests include actual Rails boot/installation, ActionMailbox persistence and mailbox processing, task orchestration, HTTP-mocked API behavior, and Worker unit tests. CI also builds and installs the packaged gem and runs a real local workerd-to-Rails check. Run the latter with:
421
-
422
- ```sh
423
- BUNDLE_GEMFILE=gemfiles/local_ingress.gemfile bundle install
424
- BUNDLE_GEMFILE=gemfiles/local_ingress.gemfile bundle exec ruby script/verify_local_ingress.rb
93
+ response.message_id
94
+ response.accepted?
425
95
  ```
426
96
 
427
- Install the Worker tooling first; Node 22+ must be on PATH (or set `NODE_BINARY` to its executable). This uses synthetic mail and temporary loopback services, not a deployed Cloudflare account. The development bundle pins JSON below 3 because current tested Rails versions require its positional-options API.
97
+ Acceptance is the start of delivery. Use [delivery events](docs/delivery-events.md)
98
+ to track delivered messages, bounces, and complaints.
99
+ See the [Ruby API reference](docs/reference.md) for attachments, raw MIME, and configuration.
428
100
 
429
- See the [verification report](docs/verification/2026-09-10.md) for evidence, the historical dogfood inventory, Rebulk integration findings, and remaining live-provider checks.
101
+ ## Guides
430
102
 
431
- A subsequent [live verification pass](docs/verification/2026-09-10-live.md) exercised isolated sending, deployed ingress, binary attachments, reply threading, real LLM processing, and delivery-event redelivery/acknowledgement under `test.rebulk.com`. The [follow-up report](docs/verification/2026-09-10-followup.md) covers authenticated recipient routing and draft-review improvements after that run. Temporary cloud resources were removed afterward. No DNS changes or RubyGems publication occurred. These dated reports do not constitute live verification of later changes.
432
-
433
- MIT license.
103
+ | Task | Guide |
104
+ | --- | --- |
105
+ | Set up sending, receiving, and tracking | [Rails quickstart](docs/getting-started.md) |
106
+ | Explore the available features | [Feature list](docs/features.md) |
107
+ | Create inboxes and aliases | [Mailboxes](docs/mailboxes.md) |
108
+ | Add mailbox administration | [Management UI](docs/management-engine.md) |
109
+ | Configure addresses and customer subdomains | [Cloudflare domain setup](templates/worker/docs/domain-setup.md) |
110
+ | Save outgoing messages and safely retry jobs | [Outbox](docs/outbox.md) |
111
+ | Track delivery and bounces | [Delivery events](docs/delivery-events.md) |
112
+ | Use separate SQLite databases | [Database tenancy](docs/activerecord-tenanted.md) |
113
+ | Connect a custom receiving endpoint | [Custom ingress](docs/custom-ingress.md) |
114
+ | Diagnose missing mail | [Troubleshooting](docs/troubleshooting.md) |
115
+ | Configure the client and command-line tasks | [Reference](docs/reference.md) |
116
+
117
+ [Security](SECURITY.md) · [Contributing](CONTRIBUTING.md) · [MIT license](LICENSE.txt)
data/SECURITY.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # Security
2
2
 
3
- The hardening described here is included in 0.2.0. Version 0.1.0 does not include
4
- these fixes. Upgrade Rails and the bundled Worker together using the
5
- [upgrade guide](docs/upgrading-0.2.md).
6
-
7
3
  Rails integration is tested on patched Rails 7.2, 8.0, and 8.1; application
8
4
  owners must update their own Rails, database adapter, and other dependencies.
9
5
  The gem does not add Rails or SQLite as runtime dependencies.
@@ -40,12 +36,8 @@ Do not place secrets or private message data in public issues.
40
36
  Ingress selects a registered tenant only after full signature verification.
41
37
  Raw ActiveRecord access remains privileged, especially in shared-database mode.
42
38
  Private tenant storage, consistent migrations and trusted job payloads are
43
- required. Drain old framework jobs before enabling database tenancy; jobs
44
- without the new tenant metadata are rejected rather than guessing a tenant.
39
+ required. Jobs without the required tenant context are rejected.
45
40
  - Development tunnels expose the ingress to the internet. Use the bundled task,
46
41
  dedicated development mail routes and test data; stop tunnels when finished.
47
42
  The Host guard restricts routing but is not a sandbox for the development app
48
43
  or a replacement for safe error pages and request logging.
49
-
50
- See [the September 11 security review](docs/verification/2026-09-11-security.md)
51
- for findings, verification, and remaining limits.
@@ -168,14 +168,3 @@ database. Missing context must continue to fail closed. Your organization's job
168
168
  fan-out should enumerate real organizations, not every database file. Applications
169
169
  without configured multi-tenancy can keep ordinary system and organization mail
170
170
  in their existing single database; this recipe does not enable tenancy by default.
171
-
172
- ## What is verified
173
-
174
- The optional CI job boots a real Rails application with `activerecord-tenanted`, runs the gem's tenant migrations in two SQLite databases, and checks identical numeric IDs remain isolated. It also verifies context restoration, refusal to create unknown tenant databases, and GlobalID's missing/wrong-tenant rejection. Separate integration tests exercise mailbox ingress, sending, jobs, and delivery-event replay.
175
-
176
- Run the library-specific smoke test locally with:
177
-
178
- ```sh
179
- BUNDLE_GEMFILE=gemfiles/tenanted.gemfile bundle install
180
- BUNDLE_GEMFILE=gemfiles/tenanted.gemfile bundle exec ruby test/support/activerecord_tenanted.rb
181
- ```
@@ -1,7 +1,5 @@
1
1
  # Use your existing inbound email pipeline
2
2
 
3
- This integration API is available in gem version 0.3.0.
4
-
5
3
  You can adopt mailbox registration without replacing your Worker, document processing, sender review, or archive. Use the gem to authenticate the request and resolve an accepted address, then keep your application's policy and processing.
6
4
 
7
5
  A useful starting point is one mailbox per site: an organization owns a receiving domain, a site owns a mailbox, and that mailbox can have several accepted addresses. This is an application convention, not a requirement of the gem. Database tenancy is optional; a single SQLite database works too.
@@ -181,7 +179,7 @@ const headers = await signedEmailHeaders({
181
179
  // Send exactly raw with these headers using your existing transport.
182
180
  ```
183
181
 
184
- Here `cloudflare-ingress.js` is your local copy of the template's `src/index.js`. `raw` and `archiveKey` come from your existing bounded reader and archive operation. See the [Worker integration instructions](../templates/worker/README.md) for supported metadata values and forwarding behavior. Upgrade the Rails receiver before enabling v3 in the Worker.
182
+ Here `cloudflare-ingress.js` is your local copy of the template's `src/index.js`. `raw` and `archiveKey` come from your existing bounded reader and archive operation. See the [Worker integration instructions](../templates/worker/README.md) for supported metadata values and forwarding behavior.
185
183
 
186
184
  In your Action Mailbox handler, read persisted metadata through the gem:
187
185
 
@@ -197,15 +195,3 @@ This reads verified Active Storage metadata, not similarly named MIME headers. A
197
195
  **A valid Worker signature proves that your trusted Worker submitted the request. It does not prove the original email sender is authentic.** Even metadata labelled `source: "cloudflare"` is an assertion made by the holder of the ingress secret. Do not copy sender-controlled MIME headers into a “trusted authentication” field. If your existing Worker has reliable authentication evidence, preserve its documented provenance and let your application decide how that evidence affects acceptance. The gem intentionally does not infer SPF/DMARC results or implement a sender allowlist.
198
196
 
199
197
  Retries with the same recipient, raw bytes, and metadata context reuse the inbound record. Aliases receive distinct membership context. Changing signed metadata produces a distinct checksum: stable values are preferable to per-attempt timestamps or random IDs. Keep downstream document extraction and external side effects idempotent too.
200
-
201
- ## Adopt incrementally
202
-
203
- 1. Inventory existing accepted addresses, organization/subdomain mappings, fallback destinations, and disabled sites. Backfill records explicitly; do not silently create a mailbox for every incoming address.
204
- 2. Confirm existing Worker routing for one organization's exact domain and activate only verified addresses. Exercise an alias, unknown address, suspended mailbox, and cross-tenant destination with synthetic mail.
205
- 3. Add request verification and preserve your current sender review, archive behavior, error responses, and size limits. Test that rejected or held mail cannot reach the processing queue.
206
- 4. Connect site provisioning to the same mailbox API used by the [management engine](management-engine.md). Scope the engine through existing host authentication and authorization.
207
- 5. Retire duplicate infrastructure only after comparing delivery and recovery behavior. Inbound adoption does not require switching your outbound provider.
208
-
209
- Keep the shared operational communications history separate from tenant-local raw emails and mailbox membership when those serve different purposes. Define who owns deletion, archive retention, deduplication, and processing status; two competing delivery ledgers make recovery harder.
210
-
211
- The gem's custom-ingress integration test exercises a custom controller against two real SQLite tenant databases, preserving binary MIME bytes and signed metadata before routing jobs run. It also verifies rejected metadata, host review, accepted-address enforcement, and retry behavior. It does not verify a deployed customer's Cloudflare DNS, Worker authentication evidence, R2 archive, or document-processing pipeline; those require that host's end-to-end tests.