# Self-hosting Mailflare (Docker) Run Mailflare as one container on your own server instead of Cloudflare Workers. The container includes the database (SQLite), file storage, job queue, real-time updates, backups and an SMTP listener. ## Prerequisites - A way to receive mail: open port 25, or Cloudflare Email Routing, Resend or Amazon SES. - A way to send mail: an SMTP relay, Cloudflare Email Sending, Resend or Amazon SES. ## Quick start ```bash git clone https://github.com/hieunc229/mailflare && cd mailflare cp .env.docker.example .env.docker ``` 1. Edit `.env.docker`: set how to [receive](#receiving-mail) and [send](#sending-mail) mail. 2. Start the app: ```bash docker compose up -d --build ``` 3. Open `http://your-host:3000/setup`, create the admin account and add your domain. All data lives in the `mailflare-data` volume (`/data` in the container). ## Example configuration Receive on port 25 and send through an SMTP relay, behind an HTTPS reverse proxy. Put this in `.env.docker`: ```bash APP_URL=https://mail.example.com TRUST_PROXY=1 SMTP_INBOUND_PORT=25 MAIL_HOSTNAME=mail.example.com SMTP_URL=smtps://user:pass@smtp.example.com:465 ``` ## Reverse proxy and HTTPS - Set `APP_URL` to the public address. Password-reset links, JMAP and passkeys depend on it (passkeys also work over a direct HTTPS connection). - Forward WebSocket upgrades for `/api/realtime`. - Set `TRUST_PROXY=1`, or all users share one login rate limit. - **Leave `TRUST_PROXY` unset if the container is exposed directly.** ## Prebuilt image (no build) Every push to `main` publishes `ghcr.io/hieunc229/mailflare` for `linux/amd64` and `linux/arm64`. You need only `docker-compose.image.yml` and `.env.docker`: ```bash docker compose -f docker-compose.image.yml up -d ``` | Tag | Meaning | | --- | --- | | `latest` | Newest build of `main` | | A version number, such as `0.5.5` | Newest build of `main` with that `package.json` version; it moves while the version stays the same | | `sha-abc1234` | One commit; never moves. Pin it to hold or roll back a version | The prebuilt image has no Turnstile (bot protection), because its site key is set at build time. To use it, set `TURNSTILE_SECRET_KEY` and build from source: ```bash docker build --build-arg NEXT_PUBLIC_TURNSTILE_SITE_KEY= . ``` With Compose, set the same build arg under `build:` in `docker-compose.yml`. ## Receiving mail Use one or both. | Method | Best when | | --- | --- | | Built-in SMTP listener (default) | Port 25 is reachable from the internet | | Cloudflare Email Routing relay | You keep MX records on Cloudflare | Resend and Amazon SES also work, per domain ([providers](providers.md)). ### Built-in SMTP listener 1. Point the domain's MX record at your server. 2. Set `MAIL_HOSTNAME` to that host name. 3. Make sure port 25 is open. Many clouds block it by default; Hetzner and most VPS providers do not. 4. Create the MX, SPF and DMARC records listed on the domain page in the app. Reject rules answer the sender with error 550. Forward rules relay through your outbound SMTP. Optional: set `SMTP_TLS_KEY` and `SMTP_TLS_CERT` (paths in the container) to enable STARTTLS. Without them, encryption in transit depends on the sender. ### Cloudflare Email Routing relay 1. Deploy the Worker in [`deploy/cloudflare-email-relay`](../deploy/cloudflare-email-relay/README.md). 2. Give the relay and the server the same `INBOUND_WEBHOOK_SECRET`. 3. Set `SMTP_INBOUND_PORT=0` to turn off the listener. The relay posts signed messages to `/api/inbound` and applies the server's reject or forward decision. ## Sending mail | Method | Set up | | --- | --- | | Any SMTP relay (host, Amazon SES, Postmark, Mailgun, Postfix) | `SMTP_URL=smtps://user:pass@host:465`, or `smtp://` with STARTTLS | | Cloudflare Email Sending (REST API, no Workers plan) | `CF_ACCOUNT_ID` and `CF_TOKEN` with Email Sending: Edit; the zone must have Email Sending set up | | Resend or Amazon SES, per domain | Credentials on the domain page or in the variables below; see [providers](providers.md) | Set `SMTP_TLS_REJECT_UNAUTHORIZED=false` only for a relay with a self-signed certificate on a private network. Per-domain Resend or SES needs `CF_TOKEN` so Mailflare can manage DNS, and `APP_URL` for receiving. ## Automatic DNS setup (optional) Give `CF_TOKEN` DNS and Email Routing edit access, and adding a domain configures Email Routing and sending for you. Without it, the DNS page lists the records to add by hand. ## Push notifications (optional) Browsers need HTTPS for push (localhost is the exception). 1. From a source checkout, run `npm run push:keys` once. 2. Set `VAPID_PUBLIC_KEY`, `VAPID_PRIVATE_KEY` and `VAPID_SUBJECT` (a `mailto:` address or your HTTPS URL). Keep the private key secret. 3. Users turn on push under **Settings → Inbox → Notifications**. ## Email assistant (optional) Set `AI_BASE_URL`, `AI_API_KEY` and `AI_MODEL`. Chat is then available. Automatic reply drafts stay off until a mailbox manager enables them in the assistant's settings. - Turn off out-of-office replies before enabling auto-drafts for a mailbox. - Auto-draft work is retried after a restart. An AI failure never rejects incoming mail. - External AI tools connect to `/mcp` with a key from **Settings → API keys**; no AI model needed. See [Email assistant and MCP](email-assistant-and-mcp.md). ## Update > **Back up first.** Migrations only move forward. Copy the `mailflare-data` volume, or download a backup from the admin Backups page. Prebuilt image: ```bash docker compose -f docker-compose.image.yml pull && docker compose -f docker-compose.image.yml up -d ``` Keep the volume and `.env.docker`. On a NAS, pull the image and recreate the container in its UI. The in-app update button is disabled. Migrations run at startup. If one fails, the container exits; see the logs. To roll back, pin an older `sha-` tag **and** restore your backup. ## Operations | Task | Note | | --- | --- | | Backups | Daily at 02:00 UTC and from the Backups page, into `/data/blobs/backups`. Back up the whole volume for a full copy. | | Logs | `docker compose logs -f mailflare` | | Queues | **Jobs live in memory and are lost on restart.** Unsent scheduled and undo-send mail is not sent. Incoming mail whose job is lost stays unparsed, and its raw file is deleted after 24 hours by the nightly cleanup. | ## Configuration reference ### Server | Variable | Default | Purpose | | --- | --- | --- | | `PORT` | `3000` | HTTP port | | `HOST` | `0.0.0.0` | HTTP bind address. Use `127.0.0.1` when a reverse proxy on the same machine is the only entry point. | | `DATA_DIR` | `/data` | SQLite database, files and backups | | `APP_URL` | request origin | Public URL. Browser changes must come from this origin. **Required for password reset**; also for read receipts, unless the domain has a tracking host. | | `TRUST_PROXY` | unset | `1` trusts `X-Forwarded-For` and `X-Forwarded-Proto` from your proxy. Without `APP_URL`, the same-origin check uses `Host` plus that scheme. | | `MAX_HTTP_HEADER_SIZE` | `65536` | Largest request headers in bytes (minimum 16384). The email relay needs room for long header chains; too small gives error 431. | | `CF_EMAIL_WORKER_NAME` | `mailflare` | Worker that new Email Routing rules target. If you change it, recreate rules that target `mailflare`. | | `TURNSTILE_SECRET_KEY` | unset | Bot protection on login and reset forms (needs `NEXT_PUBLIC_TURNSTILE_SITE_KEY` at build time) | ### Database and storage | Variable | Default | Purpose | | --- | --- | --- | | `DATABASE_URL` | unset | PostgreSQL, MySQL or libSQL instead of SQLite ([databases](databases.md)) | | `DATABASE_DRIVER` | inferred | Override the driver | | `MIGRATIONS_DIR` | `./drizzle/migrations` | Directory of startup migrations for built-in SQLite. | | `SQLITE_CACHE_KB` | unset | SQLite page cache in KiB. | | `SQLITE_MMAP_BYTES` | unset | Size of SQLite’s memory-mapped window in bytes. | | `SQLITE_SYNCHRONOUS` | unset | SQLite sync mode, `NORMAL` or `OFF`. | | `SQLITE_TEMP_STORE` | unset | Set to `memory` to keep SQLite temporary data in memory. | | `B2_KEY_ID`, `B2_APPLICATION_KEY`, `B2_BUCKET`, `B2_ENDPOINT` | unset | Backblaze B2 for files (all four), instead of `/data/blobs` ([storage](deployment.md#object-storage-r2-backblaze-b2-or-aws-s3)) | | `S3_BUCKET`, `S3_REGION`, `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY` | unset | AWS S3 for files; keys fall back to `AWS_*`. B2 wins if both are set. | ### Mail | Variable | Default | Purpose | | --- | --- | --- | | `SMTP_INBOUND_PORT` | `25` | Listener port; `0` disables | | `SMTP_INBOUND_HOST` | `0.0.0.0` | SMTP bind address. | | `MAIL_HOSTNAME` | `mail.` | MX host and SMTP banner | | `SMTP_MAX_SIZE` | 36 MiB | Largest raw incoming message (room for about 25 MB of attachments); larger mail is rejected | | `SMTP_TLS_KEY`, `SMTP_TLS_CERT` | unset | STARTTLS certificate | | `SMTP_URL` | unset | Outgoing relay | | `SMTP_TLS_REJECT_UNAUTHORIZED` | `true` | `false` trusts self-signed relay certificates | | `CF_ACCOUNT_ID`, `CF_TOKEN` | unset | Cloudflare sending, and DNS setup if the token allows | | `RESEND_API_KEY` | unset | Resend key; overrides one saved in the app | | `RESEND_BASE_URL` | `https://api.resend.com` | Use a Resend-compatible service | | `RESEND_CUSTOM_MESSAGE_ID` | `true` | `false` if that service refuses a custom `Message-ID` | | `RESEND_OMIT_HEADERS` | empty | Comma-separated headers to leave out, such as `Auto-Submitted` | | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION` | unset | Amazon SES; overrides saved credentials | | `INBOUND_WEBHOOK_SECRET` | unset | Enables `/api/inbound` for the relay | | `INBOUND_REQUIRE_SIGNED_HEADERS` | unset | `1` refuses old relays that do not sign headers with a timestamp (their requests can be replayed). Redeploy the relay first. | ### Notifications and AI | Variable | Default | Purpose | | --- | --- | --- | | `VAPID_PUBLIC_KEY` | unset | Web Push public key | | `VAPID_PRIVATE_KEY` | unset | Web Push private key | | `VAPID_SUBJECT` | unset | Web Push contact (`mailto:` or HTTPS) | | `AI_BASE_URL` | unset | OpenAI-compatible API address | | `AI_API_KEY` | unset | Key for that API | | `AI_MODEL` | `gpt-4o-mini` | Model ID | | `MCP_ENCRYPTION_KEY` | unset | Encrypts saved MCP tokens (16+ characters; see [token encryption](email-assistant-and-mcp.md#token-encryption)) | ## Run without Docker Needs Node.js 22. ```bash npm ci npm run build:node MAILFLARE_RUNTIME=node NODE_ENV=production DATA_DIR=./data node dist/server.mjs ``` Port 25 needs root or `setcap cap_net_bind_service=+ep`. Otherwise set `SMTP_INBOUND_PORT=2525` and forward the port. ## Troubleshooting | Problem | Cause | Fix | | --- | --- | --- | | No password-reset email | `APP_URL` unset | Set `APP_URL` | | Everyone hits the login limit | Proxy IP seen as client | Set `TRUST_PROXY=1` | | No incoming mail | Port 25 blocked or wrong MX | Open the port, fix MX, or use the relay | | Relay gets error 431 | Header limit too small | Raise `MAX_HTTP_HEADER_SIZE` | | Container exits after update | Migration failed | Check `docker compose logs`, restore the backup | More: [troubleshooting](troubleshooting.md). ## Related - [Providers](providers.md) - [Databases](databases.md) - [Vercel](deploy-vercel.md)