Cloudflare Email Worker + D1 + GitHub Pages frontend.
- Generate random inbox addresses via
/email/create - Receive emails through Cloudflare Email Routing and store them in D1
- Read inbox messages via
/email/:address?limit=10 - Expose sponsor channel config via
/sponsor/info - Serve frontend from
GHPAGEURL directly through Worker (/and/ui) - Auto refresh inbox in frontend every 3 seconds
- Install dependencies
npm install- Create D1 and initialize schema
wrangler d1 create <db_name>
wrangler d1 execute <db_name> --file ./sql/schema.sql- Configure
wrangler.toml
name = "sample-mail"
main = "src/index.ts"
compatibility_date = "2024-06-06"
[[d1_databases]]
binding = "DB"
database_name = "<db_name>"
database_id = "<your_database_id>"
[vars]
email_domain = "example.com"
forward_address = "a@example.com;b@example.com"
GHPAGE = "https://<your-github-username>.github.io/sample-mail/"
SPONSOR_CURRENCY = "SOL"
SPONSOR_RECEIVE_HASH = "<wallet_or_receive_hash>"- Deploy worker
npm run deploySet a catch-all rule in Cloudflare Email Routing to this Worker, for example:
*@email_domain -> sample-mail
Without this rule, emails will not be delivered to the Worker.
After enabling Cloudflare Email Routing, add or verify the DNS records shown in your Cloudflare dashboard (exact values may vary by account).
MX: Route incoming mail foremail_domainto Cloudflare Email RoutingTXT (SPF): Authorize Cloudflare mail gateway for receiving/forwardingTXT (DKIM): Enable signing verification if required by dashboardTXT (_dmarc): Recommended to reduce spoofing and spam classification
Tip: use the exact record values from the Cloudflare Email Routing page, then test /email/create and your inbound mail flow after DNS propagation.
Create inbox:
GET /email/createResponse example:
{
"success": true,
"data": {
"fetch_endpoint": "/email/abc123@example.com",
"address": "abc123@example.com",
"mode": "catch_all_worker_rule"
}
}Get messages:
GET /email/{address}?limit=10Get sponsor info:
GET /sponsor/infoResponse example:
{
"success": true,
"data": {
"channels": [
{
"name": "SOL Transfer",
"currency": "SOL",
"receive_hash": "3eTz3jCELZGjH9oJ5WT4u7jSGF98vanLgrkGwFCwYFoo"
}
]
}
}- Frontend source is
assets/index.html - Workflow deploys
assets/togh-pagesautomatically - Worker reads
GHPAGEand returns that UI from/and/ui
- If
email_domainis missing,/email/createreturns a config error forward_addresscan be empty; use;to separate multiple emails