Skip to content

Commit 6a3282f

Browse files
committed
fastmail: Complete self-hosted mail, sync, and calendar support
1 parent 406392d commit 6a3282f

123 files changed

Lines changed: 5613 additions & 7827 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,53 @@
1-
name: "Build Inbox Zero Docker Image"
2-
run-name: "Build Inbox Zero Docker Image"
3-
1+
name: Build self-hosted images
42
on:
53
push:
6-
branches: ["main"]
7-
tags: ["v*"] # Also build on version tags for releases
8-
workflow_dispatch: # Allow manual trigger
9-
10-
# Dual-arch Next.js image builds are memory-heavy on Depot. Overlapping
11-
# pushes to main previously OOMed the builders (keepalive ping timeout).
4+
branches: [main]
5+
tags: ["v*"]
6+
workflow_dispatch:
127
concurrency:
138
group: ${{ github.workflow }}-${{ github.ref }}
149
cancel-in-progress: true
15-
1610
permissions:
1711
contents: read
1812
packages: write
19-
id-token: write
20-
21-
env:
22-
DOCKER_IMAGE_REGISTRY: "ghcr.io"
23-
DOCKER_USERNAME: "elie222"
24-
DEPOT_PROJECT_ID: "2s1sh2pjrf"
25-
2613
jobs:
27-
build-docker:
28-
if: github.repository == 'elie222/inbox-zero'
29-
name: "Build Docker Image"
14+
build:
3015
runs-on: ubuntu-latest
31-
timeout-minutes: 45
32-
16+
timeout-minutes: 90
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
include:
21+
- image: inbox-zero
22+
dockerfile: docker/Dockerfile.prod
23+
- image: inbox-zero-fastmail-daemon
24+
dockerfile: apps/fastmail-eventsource-daemon/Dockerfile
3325
steps:
34-
- name: Checkout code
35-
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
26+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
27+
- name: Normalize registry owner
28+
run: echo "REGISTRY_OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_ENV"
29+
- uses: docker/setup-qemu-action@v3
30+
- uses: docker/setup-buildx-action@v3
31+
- uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
3632
with:
37-
fetch-depth: 0
38-
39-
- name: Generate Docker tags
33+
registry: ghcr.io
34+
username: ${{ github.actor }}
35+
password: ${{ secrets.GITHUB_TOKEN }}
36+
- uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5
4037
id: meta
41-
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5
4238
with:
43-
images: |
44-
ghcr.io/${{ env.DOCKER_USERNAME }}/inbox-zero
45-
${{ env.DOCKER_USERNAME }}/inbox-zero
39+
images: ghcr.io/${{ env.REGISTRY_OWNER }}/${{ matrix.image }}
4640
tags: |
47-
# Always tag with short SHA
4841
type=sha,prefix=
49-
# Tag 'latest' on main branch
5042
type=raw,value=latest,enable={{is_default_branch}}
51-
# Tag with version on v* tags (e.g., v2.26.0)
5243
type=semver,pattern={{version}}
53-
type=semver,pattern={{major}}.{{minor}}
54-
55-
- name: Login to GHCR
56-
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
57-
with:
58-
registry: ${{ env.DOCKER_IMAGE_REGISTRY }}
59-
username: ${{ env.DOCKER_USERNAME }}
60-
password: ${{ secrets.GITHUB_TOKEN }}
61-
62-
- name: Login to Docker Hub
63-
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
44+
- uses: docker/build-push-action@v6
6445
with:
65-
username: ${{ env.DOCKER_USERNAME }}
66-
password: ${{ secrets.DOCKERHUB_TOKEN }}
67-
68-
- name: Set up Depot
69-
uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1
70-
71-
- name: Build and Push Docker Image
72-
uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1
73-
with:
74-
project: ${{ env.DEPOT_PROJECT_ID }}
7546
context: .
76-
file: docker/Dockerfile.prod
47+
file: ${{ matrix.dockerfile }}
7748
platforms: linux/amd64,linux/arm64
7849
push: true
7950
tags: ${{ steps.meta.outputs.tags }}
8051
labels: ${{ steps.meta.outputs.labels }}
81-
82-
- name: Update Docker Hub Description
83-
uses: peter-evans/dockerhub-description@432a30c9e07499fd01da9f8a49f0faf9e0ca5b77 # v4
84-
with:
85-
username: ${{ env.DOCKER_USERNAME }}
86-
password: ${{ secrets.DOCKERHUB_TOKEN }}
87-
repository: ${{ env.DOCKER_USERNAME }}/inbox-zero
88-
short-description: "Inbox Zero - AI email assistant for Gmail and Outlook to reach inbox zero fast"
89-
readme-filepath: ./README.md
52+
cache-from: type=gha,scope=${{ matrix.image }}
53+
cache-to: type=gha,mode=max,scope=${{ matrix.image }}

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
For this fork’s Fastmail and Authelia deployment, see [Fastmail self-hosting](docs/FASTMAIL_SELF_HOSTING.md).
2+
13
[![](apps/web/app/opengraph-image.jpg)](https://www.getinboxzero.com)
24

35
<p align="center">
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
# Fastmail EventSource Daemon Configuration
2-
# This daemon connects to Fastmail's EventSource API for real-time email notifications
3-
# and forwards state changes to the main Inbox Zero application webhook.
4-
5-
# Required: Database URL (same as main app)
6-
DATABASE_URL=postgresql://user:password@localhost:5432/inbox_zero
7-
8-
# Required: Main application URL where the webhook endpoint is hosted
9-
MAIN_APP_URL=http://localhost:3000
10-
11-
# Required: Shared secret for authenticating with the webhook endpoint
12-
# Must match FASTMAIL_WEBHOOK_SECRET in your main app
13-
# Generate with: openssl rand -hex 32
14-
FASTMAIL_WEBHOOK_SECRET=your-secret-here
15-
16-
# Required for token refresh (when using OAuth, not app tokens)
17-
FASTMAIL_CLIENT_ID=your-fastmail-client-id
18-
FASTMAIL_CLIENT_SECRET=your-fastmail-client-secret
19-
20-
# Optional: How often to refresh the account list (in milliseconds)
21-
# Default: 300000 (5 minutes)
22-
ACCOUNT_REFRESH_INTERVAL=300000
23-
24-
# Optional: Enable debug logging
25-
DEBUG=false
1+
# Internal web address; keep this service on the private application network.
2+
MAIN_APP_URL=http://web:3000
3+
# Match the web application's secrets. Generate each with openssl rand -hex 32.
4+
INTERNAL_API_KEY=
5+
FASTMAIL_WEBHOOK_SECRET=
6+
# Refresh linked accounts and rotated tokens once per minute.
7+
ACCOUNT_REFRESH_INTERVAL=60000
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
# Fastmail EventSource Daemon
2-
# This daemon connects to Fastmail's EventSource API for real-time email notifications
3-
4-
FROM node:20-alpine
5-
6-
# Install pnpm
7-
RUN corepack enable && corepack prepare pnpm@latest --activate
8-
1+
FROM node:24-alpine AS build
92
WORKDIR /app
10-
11-
# Copy package files and prisma schema
12-
COPY package.json pnpm-lock.yaml* ./
13-
COPY prisma ./prisma/
14-
15-
# Install dependencies (this also runs prisma generate via postinstall)
16-
RUN pnpm install --frozen-lockfile
17-
18-
# Copy source files
3+
ENV pnpm_config_verify_deps_before_run=false
4+
RUN npm install -g pnpm@11.19.0
195
COPY . .
6+
RUN pnpm --filter inbox-zero-fastmail-eventsource-daemon install --frozen-lockfile --ignore-scripts --config.enableGlobalVirtualStore=false
7+
RUN pnpm --filter inbox-zero-fastmail-eventsource-daemon build
8+
RUN pnpm --filter inbox-zero-fastmail-eventsource-daemon deploy --legacy --prod --config.enableGlobalVirtualStore=false /daemon
209

21-
# Build TypeScript
22-
RUN pnpm build
23-
24-
# Run the daemon
25-
CMD ["pnpm", "start"]
10+
FROM node:24-alpine
11+
WORKDIR /app
12+
ENV NODE_ENV=production
13+
COPY --from=build --chown=node:node /daemon .
14+
COPY --from=build --chown=node:node /app/apps/fastmail-eventsource-daemon/dist ./dist
15+
USER node
16+
CMD ["node", "dist/index.js"]
Lines changed: 11 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,20 @@
1-
# Fastmail EventSource Daemon
1+
# Fastmail EventSource daemon
22

3-
A lightweight daemon that connects to Fastmail's JMAP EventSource API for real-time email notifications and forwards state changes to the main Inbox Zero application.
3+
This service receives JMAP EventSource (SSE) notifications and asks the web application to enqueue account synchronization. It needs no database access. The web application decrypts API tokens through its normal credential layer and returns them only to the authenticated internal configuration endpoint.
44

5-
## Overview
5+
Run from the repository root with Node 24 and pnpm 11.19:
66

7-
This daemon implements [JMAP EventSource](https://www.rfc-editor.org/rfc/rfc8620.html#section-7.3) (RFC 8620 section 7.3) to receive real-time push notifications from Fastmail when emails arrive or change state.
8-
9-
### Architecture
10-
11-
```
12-
┌─────────────────────────────────────────────────────────────────┐
13-
│ Self-Hosted Deployment │
14-
├─────────────────────────────────────────────────────────────────┤
15-
│ │
16-
│ ┌─────────────────────────────────────────────────────────────┐ │
17-
│ │ EventSource Daemon │ │
18-
│ │ (this service) │ │
19-
│ │ │ │
20-
│ │ For each Fastmail account: │ │
21-
│ │ 1. Connect to Fastmail EventSource │ │
22-
│ │ 2. On state change → POST to webhook │ │
23-
│ └──────────────────────────┬──────────────────────────────────┘ │
24-
│ │ │
25-
│ │ POST /api/fastmail/webhook │
26-
│ │ { emailAccountId, newState } │
27-
│ ▼ │
28-
│ ┌─────────────────────────────────────────────────────────────┐ │
29-
│ │ Next.js Application │ │
30-
│ │ │ │
31-
│ │ /api/fastmail/webhook ─────► pollFastmailAccount() │ │
32-
│ └─────────────────────────────────────────────────────────────┘ │
33-
└─────────────────────────────────────────────────────────────────┘
34-
```
35-
36-
## Requirements
37-
38-
- Node.js 20+
39-
- Access to the same PostgreSQL database as the main Inbox Zero app
40-
- Network access to the main Inbox Zero app webhook endpoint
41-
42-
## Configuration
43-
44-
Copy `.env.example` to `.env` and configure:
45-
46-
```bash
47-
# Required: Same database as main app
48-
DATABASE_URL=postgresql://user:password@localhost:5432/inbox_zero
49-
50-
# Required: Main app URL
51-
MAIN_APP_URL=http://localhost:3000
52-
53-
# Required: Shared secret (must match FASTMAIL_WEBHOOK_SECRET in main app)
54-
# Generate with: openssl rand -hex 32
55-
FASTMAIL_WEBHOOK_SECRET=your-secret-here
56-
57-
# Required for OAuth token refresh
58-
FASTMAIL_CLIENT_ID=your-fastmail-client-id
59-
FASTMAIL_CLIENT_SECRET=your-fastmail-client-secret
60-
61-
# Optional: Refresh interval (default: 5 minutes)
62-
ACCOUNT_REFRESH_INTERVAL=300000
63-
64-
# Optional: Enable debug logging
65-
DEBUG=false
66-
```
67-
68-
## Running
69-
70-
### Development
71-
72-
```bash
73-
pnpm install
74-
pnpm dev
75-
```
76-
77-
### Production
78-
79-
```bash
80-
pnpm install
81-
pnpm start
82-
```
83-
84-
### Docker
85-
86-
```bash
87-
docker build -t inbox-zero-fastmail-daemon .
88-
docker run -e DATABASE_URL=... -e MAIN_APP_URL=... -e FASTMAIL_WEBHOOK_SECRET=... inbox-zero-fastmail-daemon
89-
```
90-
91-
### Docker Compose
92-
93-
Add to your `docker-compose.yml`:
94-
95-
```yaml
96-
services:
97-
fastmail-daemon:
98-
build: ./apps/fastmail-eventsource-daemon
99-
environment:
100-
- DATABASE_URL=${DATABASE_URL}
101-
- MAIN_APP_URL=http://web:3000
102-
- FASTMAIL_WEBHOOK_SECRET=${FASTMAIL_WEBHOOK_SECRET}
103-
- FASTMAIL_CLIENT_ID=${FASTMAIL_CLIENT_ID}
104-
- FASTMAIL_CLIENT_SECRET=${FASTMAIL_CLIENT_SECRET}
105-
depends_on:
106-
- web
107-
- postgres
108-
restart: unless-stopped
7+
```sh
8+
pnpm --filter inbox-zero-fastmail-eventsource-daemon build
9+
pnpm --filter inbox-zero-fastmail-eventsource-daemon start
10910
```
11011

111-
## How It Works
112-
113-
1. **Startup**: The daemon queries the database for all Fastmail accounts with:
114-
- Valid access token
115-
- At least one enabled automation rule
116-
- Premium tier with AI access (or self-hosted bypass)
117-
118-
2. **Per Account**: For each account, it:
119-
- Fetches the JMAP session to get the EventSource URL
120-
- Opens a Server-Sent Events (SSE) connection to Fastmail
121-
- Subscribes to Email state changes
12+
Set `MAIN_APP_URL`, `INTERNAL_API_KEY`, and `FASTMAIL_WEBHOOK_SECRET` as described in `.env.example`. Keep the internal API key private. Build the container from the repository root:
12213

123-
3. **On State Change**: When Fastmail sends a state change event:
124-
- The daemon POSTs to `/api/fastmail/webhook` on the main app
125-
- The main app triggers `pollFastmailAccount()` with `forceSync: true`
126-
- Emails are processed through the rule engine
127-
128-
4. **Periodic Refresh**: Every 5 minutes (configurable), the daemon:
129-
- Re-queries the database for accounts
130-
- Starts connections for new accounts
131-
- Closes connections for removed/ineligible accounts
132-
133-
## Error Handling
134-
135-
- **Connection errors**: Exponential backoff reconnection (1s, 2s, 4s... up to 5 min)
136-
- **Token expiration (401)**: Automatic token refresh using OAuth refresh token
137-
- **Webhook failures**: Retry with exponential backoff
138-
- **Database errors**: Logged and continued (existing connections maintained)
139-
140-
## Future: PushSubscription
141-
142-
When Fastmail adds support for JMAP PushSubscription (RFC 8620 section 7.2),
143-
this daemon will become unnecessary. Fastmail will be able to POST directly
144-
to the `/api/fastmail/webhook` endpoint, which is already designed to handle this.
145-
146-
The webhook endpoint in the main app is compatible with both:
147-
1. This EventSource daemon (current)
148-
2. Future Fastmail PushSubscription webhooks
149-
150-
## Logs
151-
152-
The daemon logs to stdout/stderr. In production, capture logs with your container orchestrator or process manager.
153-
154-
Example output:
14+
```sh
15+
docker build -f apps/fastmail-eventsource-daemon/Dockerfile -t inbox-zero-fastmail-daemon .
15516
```
156-
╔═══════════════════════════════════════════════════════════════╗
157-
║ Fastmail EventSource Daemon ║
158-
║ ║
159-
║ Connects to Fastmail for real-time email notifications ║
160-
║ and forwards state changes to the main app webhook. ║
161-
╚═══════════════════════════════════════════════════════════════╝
16217

163-
Main App URL: http://localhost:3000
164-
Debug Mode: disabled
18+
The daemon refreshes accounts every minute, reconnects after disconnects or missing heartbeats, and requests recovery on every connection. Webhook delivery is bounded and retried; the independent five-minute cron poll recovers missed notifications even when the daemon is offline. The web application commits incoming message IDs and the JMAP cursor together before processing work. Failed items remain pending for retry.
16519

166-
[2024-01-15T10:30:00.000Z] [AccountManager] Starting account manager
167-
[2024-01-15T10:30:00.100Z] [AccountManager] Found 3 eligible Fastmail accounts
168-
[2024-01-15T10:30:00.200Z] [AccountManager] Adding connection for user@example.com
169-
[2024-01-15T10:30:00.500Z] [AccountManager] Connected: abc123
170-
[Stats] Connections: 3/3 active
171-
```
20+
See [Fastmail self-hosting](../../docs/FASTMAIL_SELF_HOSTING.md) for the full deployment and release checks.

0 commit comments

Comments
 (0)