# Deploying Mailflare to Cloudflare Deploy Mailflare to Cloudflare Workers, then connect your domain. For other platforms, see [Docker](deploy-docker.md) or [Vercel](deploy-vercel.md). ## Prerequisites - A domain that uses Cloudflare DNS. - A Paid Workers plan, only if you send through Cloudflare Email Sending. Resend and Amazon SES are alternatives for sending and receiving ([providers](providers.md)). ## Deploy to Cloudflare ### 1. Create `CF_TOKEN` `CF_TOKEN` is a scoped Cloudflare API token that Mailflare uses while it runs. 1. In Cloudflare, open **Manage Account → Account API Tokens** and create a token. 2. In the policy, select **Specified Domains** and pick every domain you plan to connect. 3. Add these permissions: | Permission | Access | | --- | --- | | DNS & Zones → DNS (lets setup replace conflicting MX records after you confirm) | Edit | | DNS & Zones → Zone | Read | | DNS & Zones → Zone Settings | Edit | | Email & Messaging → Email Routing Rules | Edit | 4. To send mail through Cloudflare, add a second policy for **Entire Account** with **Email & Messaging → Email Sending: Edit**. 5. To send through Cloudflare, open **Compute → Email Service → Email Sending**, buy a Paid Workers plan if you have none, then select **On-board domain** and follow the prompts. TODO: The README also lists "DNS Settings: Edit" and "Email Routing Addresses: Edit". Are they required? Copy the token secret only. Do not add `Bearer` and do not use the token ID. The token must belong to the same account as your domains. ### 2. Deploy the app 1. Click the button and sign in if asked. [![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/hieunc229/mailflare) 2. Choose the account that owns your domain. 3. Set the app name to exactly `mailflare`. **Do not rename it.** 4. Add `CF_TOKEN` when asked for runtime variables or secrets. It differs from the token Cloudflare uses to deploy. 5. Start the deployment and wait for it to finish. ### 3. Complete setup To use another database than D1, first set `DATABASE_URL` (and `DATABASE_DRIVER` if it cannot be guessed) as a Worker variable or secret (see [databases](databases.md)). 1. Open your Worker URL. Go to `/setup` if you are not redirected. 2. Let Mailflare check the configuration and initialize the empty database. 3. Create the first admin account. ### 4. Connect your domain 1. Enter your domain. 2. Wait while Mailflare enables Email Routing and writes the routing and sending DNS records. 3. Choose the address of your first mailbox and finish. 4. Send a test message to the new address and check the inbox. If you set up DKIM and DMARC records for Cloudflare sending, open **Admin → Domains** and expand the domain. ## Add more domains Open **Admin → Domains → New domain**, enter the hostname and choose the receiving service. | Service | What happens | | --- | --- | | Cloudflare | Mailflare configures Email Routing and Email Sending | | Resend or Amazon SES | Add the credentials, then run setup on the domain page ([providers](providers.md)) | Every domain still needs `CF_TOKEN`, because Cloudflare manages its DNS. For Resend or SES **receiving**, first set `APP_URL` to the app's public HTTPS address. ## Deploy with an AI coding agent Paste the prompt below into an agent with terminal access. Give it your account ID and **two separate tokens** through its secret input. Never put them in a public chat, repository or committed file. | Token | Used as | Permissions | | --- | --- | --- | | Deployment | `CLOUDFLARE_API_TOKEN` (Wrangler) | On the target account: Workers Scripts Edit (or Workers Admin), D1 Edit, Workers R2 Storage Edit, Queues Edit, Account Settings Read. Add Workers Routes Edit on a zone only to attach a custom domain or route. | | Runtime | Worker secret `CF_TOKEN` | The [`CF_TOKEN` permissions](#1-create-cf_token), including Email Sending Edit to send mail, covering your zones | See Cloudflare's [token permissions](https://developers.cloudflare.com/fundamentals/api/reference/permissions/) and [Workers roles](https://developers.cloudflare.com/workers/authorization/workers/). ```text Install Mailflare from https://github.com/hieunc229/mailflare in my Cloudflare account. Ask me for my Cloudflare account ID, a scoped deployment API token, and a separate runtime CF_TOKEN through a secret input. Never print, commit, or place either token in a command argument or a tracked file. Use the deployment token only for Wrangler authentication (CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID). Read README.md, docs/deploy-cloudflare.md, and wrangler.jsonc first. Keep the Worker name exactly mailflare. In the selected account, create or reuse the D1 database mailflare, R2 bucket mailflare-raw, and Queues mailflare-inbound, mailflare-outbound, and mailflare-agent. Set the D1 database_id in the local Wrangler config without committing that account-specific ID. Install dependencies, run npm run deploy, and set the runtime CF_TOKEN as a Worker secret. Do not run remote D1 migrations manually; the /setup flow initializes the database. Give me the deployed URL and any remaining Cloudflare account actions. I will open /setup, create the first admin account, and connect my domain there. ``` ## Deploy manually Configure the Cloudflare bindings in `wrangler.jsonc`, then run: ```bash npm install npm run deploy ``` This builds the whole Worker and uploads it with Wrangler. It does not change the database. Deploy the whole Worker: `worker.ts` also handles inbound email, queues, scheduled backups and real-time updates. To apply pending migrations by hand (recovery only): ```bash npm run db:migrate:remote ``` This needs your account's `database_id` in your local `wrangler.jsonc`. **Do not commit that ID** to a reusable repository. ## Web Push notifications Push sends new-mail notifications while the browser is closed. 1. Run `npm run push:keys` once. 2. Store `VAPID_PRIVATE_KEY` as a Worker secret. 3. Set `VAPID_PUBLIC_KEY` and `VAPID_SUBJECT` as Worker variables. The subject is a `mailto:` address or your public HTTPS URL. Keep the same keys across deployments, or existing browser subscriptions stop working. ## Database backups Mailflare exports database records as JSON into the storage bucket. Choose the schedule under **Admin → Backups**. A cron trigger runs daily at 02:00 UTC. You can also start a backup there by hand. - Redeploy with `npm run deploy` when the cron trigger is added or changed. - Upgrading an old installation? After the cron trigger works, you can remove the old Workflow. This deletes its run history, not your backup files or backup list. ```bash npx wrangler workflows delete mailflare-database-backup ``` ## Email assistant and MCP The assistant uses the Workers AI `AI` binding and the `mailflare-agent` queue. 1. Create the queue in your account before deploying a configuration that declares it. 2. On an existing database, apply pending migrations (needs `0032_add_agentic_mail.sql`). 3. In the inbox, open **Assistant → Settings** for a mailbox, choose its reviewer and enable it. Auto-drafting is a separate opt-in. It skips spam, automated mail and mailboxes with out-of-office replies on. The reviewer must open each draft and confirm it before it is sent. The five-minute cron recovers pending drafts. External AI tools connect to `https:///mcp`, and outside MCP servers need `MCP_ENCRYPTION_KEY`. See [Email assistant and MCP](email-assistant-and-mcp.md). ## Related - [Storage, updates and license](deployment.md) - [Troubleshooting](troubleshooting.md) - [Providers](providers.md) - [Databases](databases.md)