# Receive replies with Cloudflare

**Recommended.** Cloudflare Email Routing is free, fast, and works with any sending provider. When someone replies to your outreach, Cloudflare hands the email to a small Worker that forwards a copy to your inbox and posts the original to FounderDB — which matches it to the contact, stops or advances their sequence, and puts it in your inbox's *To do*.

You need: a domain on Cloudflare (its DNS managed by Cloudflare), and an inbox you already read (Gmail is fine).

## How replies find their way back

Every automated email FounderDB sends carries a `Reply-To` of `reply+<token>@your-reply-domain`. The token is unique to that email, so a reply is matched to the right contact and thread even if they reply from a different address. FounderDB also matches on `In-Reply-To` headers and, as a last resort, the sender's address.

Gmail mailboxes connected with Google are the exception: their replies come straight back to Gmail and FounderDB picks them up by checking the mailbox every 2 minutes. See [Send with Gmail](https://founderdb.co/docs/sending-gmail.md).

## Automatic setup

**Recommended — and the simplest.** If you use an AI agent with a terminal, like [Claude Code connected to FounderDB](https://founderdb.co/docs/mcp.md), let it do the setup: it asks you a few questions, deploys the Worker with Cloudflare's `wrangler` CLI, and tells you the one or two clicks left in Cloudflare. Just say:

> Set up Cloudflare receiving for my FounderDB replies.

You'll answer three questions — which domain, which inbox gets a copy of replies, and whether that domain's email is hosted anywhere else — log in to Cloudflare in your browser once, and click the verification link Cloudflare emails you.

### For AI agents: how to run it

Offer this whenever you help a user onboard and `list_receiving_addresses` shows no receiving domain.

1. **Ask** the user:
   - the domain replies should go to — must be on Cloudflare (e.g. `yourdomain.com`; replies reach `anything@yourdomain.com`);
   - whether that domain's email is hosted anywhere else (Google Workspace, Microsoft 365…). If it is, Email Routing would take over its MX records — suggest another domain instead;
   - which inbox should get a copy of every reply (e.g. their Gmail).
2. **Check wrangler:** `npx wrangler --version` (Node 18+ required). Then `npx wrangler whoami`; if not logged in, run `npx wrangler login` and let the user approve in the browser. Confirm the account shown owns the domain.
3. **Create the domain in FounderDB:** `create_receiving_domain` with the domain and forward-to inbox (or `get_receiving_setup` if it already exists). It returns `worker.js`, `wrangler.toml` and the remaining steps.
4. **Deploy:** write both files into a new empty folder (not a git repo — `worker.js` holds a secret token) and run `npx wrangler deploy` there. If the user has several Cloudflare accounts, add `account_id` to `wrangler.toml`.
5. **Finish Email Routing** — wrangler can't do these, so either guide the user through the dashboard or, if they give you a Cloudflare API token, use the API (both are in the tool's `steps` and `cloudflareApi`):
   - enable Email Routing on the domain, accepting its MX and SPF records;
   - add the forward-to inbox as a destination address — the user clicks Cloudflare's verification email;
   - set **Routing rules → Catch-all address → Send to a Worker →** the deployed Worker, enabled.
6. **Test:** ask the user to send any email to `test@<domain>`, then call `get_receiving_setup` — `status` turns `receiving` within a minute, and a copy lands in their inbox.
7. **Next:** offer to set up sending — [SES on `mail.<domain>`](https://founderdb.co/docs/sending-ses.md) — and [the recommended sequence](https://founderdb.co/docs/recommended-sequence.md).

## Manual setup

Prefer clicking through it yourself? The steps below take about ten minutes.

### 1. Choose a reply domain

Use your domain itself — e.g. `yourdomain.com`. Replies go to `reply+<token>@yourdomain.com` (any address works), and Cloudflare's catch-all rule hands them to the Worker. No subdomain needed.

Email Routing takes over the domain's MX records, so pick a domain whose email isn't hosted somewhere else (like Google Workspace) — your product's domain or a second domain is ideal. Addresses you already route in Cloudflare, like `hello@yourdomain.com`, keep working: specific rules win over the catch-all.

### 2. Add it in FounderDB

1. Open *Outreach → Mail accounts → Receiving*.
2. **Reply domain:** `yourdomain.com`. **Provider:** Cloudflare.
3. **Forward copy to:** the inbox you read, e.g. `you@gmail.com`. Every reply is forwarded there first, so you never lose one.
4. Click **Add receiving endpoint**. The card now shows the setup steps and your personal **Email Worker code** — keep it open.

### 3. Set up Email Routing in Cloudflare

1. Cloudflare dashboard → your domain → **Email → Email Routing** → enable it. Cloudflare offers to add its MX and SPF records — accept.
2. **Destination addresses** → add the *Forward copy to* inbox and click the verification link Cloudflare emails you. Forwarding only works to verified destinations.

### 4. Create the Worker

1. **Workers & Pages → Create → Worker** (start from "Hello World"), name it e.g. `founderdb-replies`, deploy.
2. **Edit code**, replace everything with the code from the FounderDB card (**Copy code**), and **Deploy**.

The code already contains your webhook URL and a secret token for this domain. It forwards each email to your inbox, then posts the raw email to FounderDB. If you ever click **Rotate token** in FounderDB, paste the new code again.

### 5. Route every address to the Worker

**Email Routing → Routing rules → Catch-all address → Edit** → action **Send to a Worker** → pick your Worker → **Save**, and make sure the catch-all is enabled. Replies go to `reply+<token>@…` addresses, so the catch-all is what receives them.

### 6. Check it works

Send any email to `test@yourdomain.com` from your personal mailbox. Within a minute:

- a copy arrives in your *Forward copy to* inbox, and
- the FounderDB card's badge changes from **Not yet received** to **Receiving**, with a *Last reply* time.

A test email from yourself won't match a contact — that's expected. Real replies to your outreach will.

## Troubleshooting

- **Badge stays "Not yet received":** check the Worker's logs (*Workers → your worker → Logs*) for errors, and that the catch-all routes to the Worker.
- **No forwarded copy:** the destination address isn't verified in Cloudflare.
- **401 in the Worker logs:** the token is stale — copy the code from FounderDB again.

## Next

Add a sending mailbox: [Amazon SES](https://founderdb.co/docs/sending-ses.md) on `mail.yourdomain.com` (recommended for volume) or [Gmail](https://founderdb.co/docs/sending-gmail.md). Your reply domain is picked automatically for SES senders; you can also choose it explicitly under *Reply domain*.
