📧 Email Client for Cloudflare Workers

A complete email client built with Cloudflare Workers, D1 database, and R2 storage, protected by Cloudflare Access SSO.

Features

Quick Start

1. Install Dependencies

cd email-client
npm install

2. Login to Cloudflare

npx wrangler login

3. Configure Email Sender

Edit wrangler.toml - no changes needed for send_email unless you want to restrict destinations.

4. Deploy

npm run deploy

5. Enable Cloudflare Access (SSO Protection)

This is the key step to protect your email client with Cloudflare login:

  1. Go to Cloudflare DashboardWorkers & Pages
  2. Click on your email-client worker
  3. Go to SettingsDomains & Routes
  4. Find your workers.dev URL and click "Enable Cloudflare Access"
  5. In the modal that appears, note:
    • Team Domain: https://YOUR-TEAM.cloudflareaccess.com
    • Application AUD: A long string like abc123...
  6. Configure who can access (by default, only you)

6. Configure JWT Validation

Add the values from step 5 to your wrangler.toml:

[vars]
TEAM_DOMAIN = "https://YOUR-TEAM.cloudflareaccess.com"
AUD = "your-application-aud-string-here"

Or set them via Dashboard: Workers & Pagesemail-clientSettingsVariables

7. Redeploy

npm run deploy

8. Create Email Route

  1. Go to EmailEmail RoutingEmail Workers
  2. Find email-client worker
  3. Click "Create route"
  4. Enter your email address (e.g., hello@yourdomain.com)
  5. Save

9. Access Your Email Client

Visit: https://email-client.YOUR-SUBDOMAIN.workers.dev/ui

You'll be prompted to log in with Cloudflare Access (same account as your Cloudflare dashboard).

How Authentication Works

  1. When you visit the email client, Cloudflare Access intercepts the request
  2. If not logged in, you're redirected to Cloudflare's login page
  3. After authentication, Cloudflare adds a signed JWT token to requests
  4. The Worker validates this JWT to ensure you're authenticated
  5. Only authenticated users can view or send emails

API Endpoints

All endpoints require Cloudflare Access authentication.

Method Path Description
GET / List all emails
GET /ui Web interface
GET /email/:id Get specific email
POST /email/:id/read Mark as read
DELETE /email/:id Delete email
GET /attachment/:id Download attachment
POST /send Send email

Troubleshooting

"Authentication required" error?

Can't access after enabling Access?

Emails not received?

Security Notes