mailwartDocs
Docs/Getting Started7 min read

System Architecture

Container topology, Docker network bridges, Traefik ingress, and dual runtime engine.


01 - System Architecture & Infrastructure

1. Overview & Purpose

mailwart is an administrative console designed for operators managing a modern Stalwart Mail Server deployment. It fills the gap between low-level MTA configuration and day-to-day email directory operations by providing:

  • Multi-domain directory and mailbox management.
  • CPanel-grade email routing, catch-all handling, and forwarders.
  • Automated Cloudflare DNS management and DNS health verification.
  • Seamless, zero-password Roundcube Webmail single sign-on (SSO).
  • An integrated AI Copilot with 48 automated administrative tools.
  • A 10-point email deliverability testing suite with blacklist monitoring.
  • Role-based access control with domain ownership and admin impersonation.

Non-Goals

  • Not an in-app email client: mailwart does not render inboxes or compose interfaces; Roundcube handles all end-user email reading and sending.
  • Not a replacement for Stalwart Admin: Low-level ACME certificates, network socket bindings, rate limit rules, and MTA queues are deep-linked to the native Stalwart Admin UI.

2. Container Topology & Docker Compose

The production deployment consists of three co-located containers managed via Dokploy Docker Compose, interfacing with external Traefik and Stalwart services.

Diagram
Rendering diagram...

Container Specifications

Container NameBase ImageRole & ResponsibilitiesVolume Mounts
email-managerNode.js 22 Alpine (multi-stage Next.js build)Web UI, Server Actions, API route handlers, AI Copilot, Cloudflare DNS client, background sync.mailwart_audit:/data
email-manager-roundcubeCustom ./roundcube/Dockerfile on roundcube/roundcubemail:latestApache + PHP 8 webmail frontend with custom em_sso plugin and managesieve plugin enabled.roundcube_db:/var/roundcube/db
mailwart-postgrespostgres:16-alpineRelational store for users, RBAC domain ownership, audit logs, delivery logs, Copilot settings, and OTP tokens.mailwart_postgres:/var/lib/postgresql/data

3. Network Architecture & Ingress

Docker Networks

  1. default (Bridge): Isolated internal network interconnecting email-manager, email-manager-roundcube, and mailwart-postgres.
  2. dokploy-network (External): Managed by Dokploy. Traefik routes inbound HTTP/HTTPS traffic to containers on this network.
  3. stalwart-net (External): Bridges mailwart and Roundcube directly to the Stalwart container (stalwart-mail), enabling private in-cluster communication on ports 8080 (JMAP), 993 (IMAP), 587 (SMTP Submission), and 4190 (ManageSieve).

Traefik Routing Rules

ServiceTraefik Router RulePriorityMiddleware / Target
Console UIHost('email.yehia.dev')1Routes to email-manager:3000
Console WebmailHost('email.yehia.dev') && PathPrefix('/webmail')110Strips prefix /webmail → routes to roundcube:80
Direct Webmail (HTTP)HostRegexp('^webmail\..+$') || Host('mail.zokm.me')100Inbound HTTP → routes directly to roundcube:80
Direct Webmail (HTTPS)HostRegexp('^webmail\..+$') || Host('mail.zokm.me')100Inbound HTTPS + TLS Let's Encrypt → roundcube:80

4. Application Runtime Boundaries

Mailwart strictly enforces Next.js App Router boundaries to ensure server security and performance:

Diagram
Rendering diagram...

5. Dual Runtime Engine: Live vs. Mock

Mailwart includes a transparent dual runtime engine that allows developers to run, test, and preview the full application without a live Stalwart server or PostgreSQL instance.

Diagram
Rendering diagram...

Detection Logic (src/lib/env.ts & src/lib/actions/result.ts)

  1. Live Mode: Active when STALWART_JMAP_URL, STALWART_ADMIN_USER, and STALWART_ADMIN_PASSWORD are present in environment variables.
  2. Mock Mode: Automatically activated when any of the three keys are missing. src/lib/stalwart/mock.ts provides pre-seeded domains (example.com, example.net), mailboxes, forwarders, autoresponders, app passwords, and delivery logs.
  3. Banner Notification: The DataSourceBanner component informs the operator whether the displayed data reflects live server state or mock test data.

6. Security & Credential Isolation

  • Server-Only Credentials: Stalwart administrator credentials, database passwords, Cloudflare tokens, session secrets, and LLM API keys are enforced with import "server-only". They can never be bundled or sent to the client.
  • Encrypted Session Cookies: Authentication uses iron-session with AES-256-GCM sealed cookies (SESSION_SECRET). The cookie contains only the session metadata (userId, username, role), never the Stalwart password.
  • Zero Plaintext Storage: Console user passwords use salted SHA-256 with cryptographically generated salts. Stalwart account creation displays the generated password once and does not retain it in the database.
  • Short-Lived SSO Tokens: Roundcube SSO tokens expire within 120 seconds and are bound to the specific mailbox and audience.