Skip to content

Email for professionals and teams

Update variable names

Hieu Nguyen (Jack)301cf0e97016
diff --git a/README.md b/README.md
index 66cdece..ea1b58c 100644
--- a/README.md
+++ b/README.md
@@ -65,8 +65,25 @@ Required setup values:
- `CF_ACCOUNT_ID` — optional unless your token can access multiple accounts.
- `CF_EMAIL_WORKER_NAME` — must match the Worker name in `wrangler.jsonc`; default is `mailflare`.
+If you rename the Worker, also update related literal resource names in `wrangler.jsonc`: `name`, `services[].service` for `WORKER_SELF_REFERENCE`, `CF_EMAIL_WORKER_NAME`, and any D1/R2/Queue names you want renamed. Cloudflare service bindings require the target Worker name to exist exactly; they cannot currently reference `name` dynamically.
+
After deployment, route inbound mail to the Worker in Cloudflare Email Routing.
+### Cloudflare token troubleshooting
+
+If onboarding fails with `Cloudflare API 403 ... code 9109: Invalid access token`, Cloudflare rejected the credential before checking domain permissions.
+
+Verify the token:
+
+```bash
+curl "https://api.cloudflare.com/client/v4/user/tokens/verify" \
+ -H "Authorization: Bearer <CF_TOKEN>"
+```
+
+The response should include `"success": true` and `"status": "active"`. In `.dev.vars` or deploy settings, set `CF_TOKEN` to the token secret value only. Do not include the word `Bearer`, do not use the token ID, and do not put a Global API Key in `CF_TOKEN`. For a Global API Key, set both `CLOUDFLARE_EMAIL` and `CLOUDFLARE_API_KEY` instead.
+
+Also check whether the token has an expiration, a `not_before` time, or client IP restrictions. If you changed deploy variables in Cloudflare, redeploy so the Worker receives the new values.
+
### Manual deploy
```bash
diff --git a/src/lib/cloudflare-api-utils.ts b/src/lib/cloudflare-api-utils.ts
index d75b6e5..245f41c 100644
--- a/src/lib/cloudflare-api-utils.ts
+++ b/src/lib/cloudflare-api-utils.ts
@@ -46,10 +46,16 @@ export function formatCloudflareError(path: string, status: number, statusText:
}
export function getCloudflareAuthHint(errors: CfApiError[]) {
- const hasAuthError = errors.some((error) => error.code === 10000 || /auth/i.test(error.message));
+ const hasAuthError = errors.some(
+ (error) =>
+ error.code === 10000 ||
+ error.code === 9109 ||
+ /auth/i.test(error.message) ||
+ /token/i.test(error.message),
+ );
if (!hasAuthError) return "";
- return " Use CF_TOKEN for a scoped API token, or CLOUDFLARE_API_KEY plus CLOUDFLARE_EMAIL for a Global API Key.";
+ return " Verify CF_TOKEN with `curl https://api.cloudflare.com/client/v4/user/tokens/verify -H \"Authorization: Bearer <token>\"`. Use the token secret value without `Bearer`, or use CLOUDFLARE_API_KEY plus CLOUDFLARE_EMAIL for a Global API Key.";
}
export function getEmailWorkerName(env: CloudflareEnv): string {
diff --git a/worker-utils.ts b/worker-utils.ts
new file mode 100644
index 0000000..3499d8e
--- /dev/null
+++ b/worker-utils.ts
@@ -0,0 +1,11 @@
+import type { InboundQueueMessage } from "./src/lib/email/inbound";
+
+export function isInboundQueueMessage(payload: unknown): payload is InboundQueueMessage {
+ return (
+ typeof payload === "object" &&
+ payload !== null &&
+ "rawR2Key" in payload &&
+ "from" in payload &&
+ "to" in payload
+ );
+}
diff --git a/worker.ts b/worker.ts
index fdd98cc..8c24d02 100644
--- a/worker.ts
+++ b/worker.ts
@@ -6,6 +6,7 @@ import {
type InboundQueueMessage,
} from "./src/lib/email/inbound";
import { processOutboundQueue, type OutboundQueueMessage } from "./src/lib/email/send";
+import { isInboundQueueMessage } from "./worker-utils";
export default {
fetch: nextHandler.fetch,
@@ -27,11 +28,10 @@ export default {
},
async queue(batch: MessageBatch, env: CloudflareEnv): Promise<void> {
- const isInbound = batch.queue === "mailflare-inbound";
for (const msg of batch.messages) {
try {
- if (isInbound) {
- await processInboundMessage(env, msg.body as InboundQueueMessage);
+ if (isInboundQueueMessage(msg.body)) {
+ await processInboundMessage(env, msg.body);
} else {
await processOutboundQueue(env, msg.body as OutboundQueueMessage);
}