# afn4nx/privatemail architecture

> Architecture diagram of the GitHub repository afn4nx/privatemail, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/afn4nx/privatemail
- Repository: https://github.com/afn4nx/privatemail

## Overview

PrivateMail is a self-hosted, receive-only IMAP workspace. A signed-in user views the inbox; the application synchronizes selected mailboxes, indexes messages locally, and fetches a selected message for reading. Separate flows manage aliases, mailbox/domain settings, sessions, setup, and encrypted migration. The graph also shows authentication/security and shared storage as internal layers. Relationships are limited to source-index references, sampled calls, and README-documented behavior; unsampled implementation details are not inferred.

Main entry points: [`app/Database.php`](https://github.com/afn4nx/privatemail/blob/main/app/Database.php) (SQLite index), [`app/MailService.php`](https://github.com/afn4nx/privatemail/blob/main/app/MailService.php) (IMAP mail service), [`app/MailboxRegistry.php`](https://github.com/afn4nx/privatemail/blob/main/app/MailboxRegistry.php) (Mailbox registry), [`index.php`](https://github.com/afn4nx/privatemail/blob/main/index.php) (Inbox and reader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["Mail experience"]
  node_inbox["Inbox and reader<br/>[index.php]"]
  node_mail_api["Mail endpoints"]
  node_classifier["Mail classifier<br/>[MailClassifier.php]"]
  node_mail_service["IMAP mail service<br/>[MailService.php]"]
end

subgraph group_accounts["Mailbox and aliases"]
  node_alias_ui["Alias management<br/>[aliases.php]"]
  node_alias_api["Alias endpoint<br/>[aliases.php]"]
  node_mailbox_registry["Mailbox registry"]
  node_multi_mailbox["Mailbox aggregation<br/>[MultiMailbox.php]"]
end

subgraph group_operations["Workspace operations"]
  node_settings_ui["Workspace settings<br/>[settings.php]"]
  node_settings_service["Settings service"]
  node_setup["Setup wizard<br/>[setup.php]"]
  node_setup_service["Setup service<br/>[SetupService.php]"]
  node_backup_ui["Backup and migration<br/>[backup.php]"]
  node_backup_service["Backup service<br/>[BackupService.php]"]
  node_sessions_ui["Device sessions<br/>[sessions.php]"]
  node_sessions_view["Sessions view<br/>[SessionsView.php]"]
end

subgraph group_foundation["Shared foundation"]
  node_session_registry["Session registry"]
  node_auth["Authentication<br/>[Auth.php]"]
  node_auth_robot["Login challenge<br/>[AuthRobot.php]"]
  node_security["Security utilities<br/>[Security.php]"]
  node_database[("SQLite index<br/>[Database.php]")]
  node_bootstrap["Application bootstrap<br/>[bootstrap.php]"]
  node_layout["Shared layout<br/>[Layout.php]"]
  node_navigation["Navigation<br/>[Navigation.php]"]
end

node_user(("Workspace user"))
node_imap["IMAP server"]
node_sqlite[("SQLite storage")]

node_user -->|"opens"| node_inbox
node_inbox -->|"synchronizes"| node_multi_mailbox
node_inbox -->|"queries messages"| node_database
node_inbox -->|"fetches message"| node_mail_service
node_mail_service -->|"reads mail"| node_imap
node_mail_service -->|"updates index"| node_database
node_mail_service -->|"classifies mail"| node_classifier
node_multi_mailbox -->|"selects accounts"| node_mailbox_registry
node_multi_mailbox -->|"dispatches sync"| node_mail_service
node_multi_mailbox -->|"reads status"| node_database
node_user -->|"requests actions"| node_mail_api
node_mail_api -->|"invokes mail actions"| node_mail_service
node_mail_api -->|"requests status"| node_multi_mailbox
node_mail_api -->|"reads and writes"| node_database
node_user -->|"creates aliases"| node_alias_ui
node_alias_ui -->|"maps domains"| node_mailbox_registry
node_alias_api -->|"reads accounts"| node_mailbox_registry
node_alias_api -->|"reads aliases"| node_database
node_user -->|"configures workspace"| node_settings_ui
node_settings_ui -->|"validates settings"| node_settings_service
node_settings_ui -->|"manages mailboxes"| node_mailbox_registry
node_user -->|"installs workspace"| node_setup
node_setup -->|"runs setup"| node_setup_service
node_setup_service -->|"initializes data"| node_database
node_user -->|"migrates settings"| node_backup_ui
node_backup_ui -->|"exports and imports"| node_backup_service
node_backup_service -->|"snapshots state"| node_database
node_backup_service -->|"snapshots accounts"| node_mailbox_registry
node_user -->|"manages devices"| node_sessions_ui
node_sessions_ui -->|"renders sessions"| node_sessions_view
node_sessions_view -->|"lists sessions"| node_session_registry
node_auth -->|"validates session"| node_session_registry
node_auth -->|"checks credentials"| node_database
node_auth -->|"returns API errors"| node_security
node_user -->|"completes challenge"| node_auth_robot
node_bootstrap -->|"initializes schema"| node_database
node_bootstrap -->|"initializes registry"| node_mailbox_registry
node_bootstrap -->|"loads authentication"| node_auth
node_bootstrap -->|"loads layout"| node_layout
node_database -->|"persists index"| node_sqlite
node_inbox -->|"renders navigation"| node_navigation

click node_inbox "https://github.com/afn4nx/privatemail/blob/main/index.php"
click node_mail_api "https://github.com/afn4nx/privatemail/tree/main/api"
click node_classifier "https://github.com/afn4nx/privatemail/blob/main/app/MailClassifier.php"
click node_mail_service "https://github.com/afn4nx/privatemail/blob/main/app/MailService.php"
click node_alias_ui "https://github.com/afn4nx/privatemail/blob/main/aliases.php"
click node_alias_api "https://github.com/afn4nx/privatemail/blob/main/api/aliases.php"
click node_mailbox_registry "https://github.com/afn4nx/privatemail/blob/main/app/MailboxRegistry.php"
click node_multi_mailbox "https://github.com/afn4nx/privatemail/blob/main/app/MultiMailbox.php"
click node_settings_ui "https://github.com/afn4nx/privatemail/blob/main/settings.php"
click node_settings_service "https://github.com/afn4nx/privatemail/blob/main/app/SettingsService.php"
click node_setup "https://github.com/afn4nx/privatemail/blob/main/setup.php"
click node_setup_service "https://github.com/afn4nx/privatemail/blob/main/app/SetupService.php"
click node_backup_ui "https://github.com/afn4nx/privatemail/blob/main/backup.php"
click node_backup_service "https://github.com/afn4nx/privatemail/blob/main/app/BackupService.php"
click node_sessions_ui "https://github.com/afn4nx/privatemail/blob/main/sessions.php"
click node_sessions_view "https://github.com/afn4nx/privatemail/blob/main/app/SessionsView.php"
click node_session_registry "https://github.com/afn4nx/privatemail/blob/main/app/SessionRegistry.php"
click node_auth "https://github.com/afn4nx/privatemail/blob/main/app/Auth.php"
click node_auth_robot "https://github.com/afn4nx/privatemail/blob/main/app/AuthRobot.php"
click node_security "https://github.com/afn4nx/privatemail/blob/main/app/Security.php"
click node_database "https://github.com/afn4nx/privatemail/blob/main/app/Database.php"
click node_bootstrap "https://github.com/afn4nx/privatemail/blob/main/bootstrap.php"
click node_layout "https://github.com/afn4nx/privatemail/blob/main/app/Layout.php"
click node_navigation "https://github.com/afn4nx/privatemail/blob/main/app/Navigation.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_inbox,node_mail_api,node_classifier,node_mail_service,node_user toneBlue
class node_alias_ui,node_alias_api,node_mailbox_registry,node_multi_mailbox,node_sqlite toneAmber
class node_settings_ui,node_settings_service,node_setup,node_setup_service,node_backup_ui,node_backup_service,node_sessions_ui,node_sessions_view,node_imap toneMint
class node_session_registry,node_auth,node_auth_robot,node_security,node_database,node_bootstrap,node_layout,node_navigation toneRose
```

## Components

### Mail experience

- [Inbox and reader](https://github.com/afn4nx/privatemail/blob/main/index.php): `index.php`
- [Mail endpoints](https://github.com/afn4nx/privatemail/tree/main/api): `api`
- [Mail classifier](https://github.com/afn4nx/privatemail/blob/main/app/MailClassifier.php): `app/MailClassifier.php`
- [IMAP mail service](https://github.com/afn4nx/privatemail/blob/main/app/MailService.php): `app/MailService.php`

### Mailbox and aliases

- [Alias management](https://github.com/afn4nx/privatemail/blob/main/aliases.php): `aliases.php`
- [Alias endpoint](https://github.com/afn4nx/privatemail/blob/main/api/aliases.php): `api/aliases.php`
- [Mailbox registry](https://github.com/afn4nx/privatemail/blob/main/app/MailboxRegistry.php): `app/MailboxRegistry.php`
- [Mailbox aggregation](https://github.com/afn4nx/privatemail/blob/main/app/MultiMailbox.php): `app/MultiMailbox.php`

### Workspace operations

- [Workspace settings](https://github.com/afn4nx/privatemail/blob/main/settings.php): `settings.php`
- [Settings service](https://github.com/afn4nx/privatemail/blob/main/app/SettingsService.php): `app/SettingsService.php`
- [Setup wizard](https://github.com/afn4nx/privatemail/blob/main/setup.php): `setup.php`
- [Setup service](https://github.com/afn4nx/privatemail/blob/main/app/SetupService.php): `app/SetupService.php`
- [Backup and migration](https://github.com/afn4nx/privatemail/blob/main/backup.php): `backup.php`
- [Backup service](https://github.com/afn4nx/privatemail/blob/main/app/BackupService.php): `app/BackupService.php`
- [Device sessions](https://github.com/afn4nx/privatemail/blob/main/sessions.php): `sessions.php`
- [Sessions view](https://github.com/afn4nx/privatemail/blob/main/app/SessionsView.php): `app/SessionsView.php`

### Shared foundation

- [Session registry](https://github.com/afn4nx/privatemail/blob/main/app/SessionRegistry.php): `app/SessionRegistry.php`
- [Authentication](https://github.com/afn4nx/privatemail/blob/main/app/Auth.php): `app/Auth.php`
- [Login challenge](https://github.com/afn4nx/privatemail/blob/main/app/AuthRobot.php): `app/AuthRobot.php`
- [Security utilities](https://github.com/afn4nx/privatemail/blob/main/app/Security.php): `app/Security.php`
- [SQLite index](https://github.com/afn4nx/privatemail/blob/main/app/Database.php): `app/Database.php`
- [Application bootstrap](https://github.com/afn4nx/privatemail/blob/main/bootstrap.php): `bootstrap.php`
- [Shared layout](https://github.com/afn4nx/privatemail/blob/main/app/Layout.php): `app/Layout.php`
- [Navigation](https://github.com/afn4nx/privatemail/blob/main/app/Navigation.php): `app/Navigation.php`

### Other components

- **Workspace user**
- **IMAP server**
- **SQLite storage**

## Connections

- Workspace user → Inbox and reader: opens (evidence: `README.md`)
- Inbox and reader → Mailbox aggregation: synchronizes (evidence: `index.php`)
- Inbox and reader → SQLite index: queries messages (evidence: `index.php`)
- Inbox and reader → IMAP mail service: fetches message (evidence: `index.php`)
- IMAP mail service → IMAP server: reads mail (evidence: `README.md`)
- IMAP mail service → SQLite index: updates index (evidence: `app/MailService.php`)
- IMAP mail service → Mail classifier: classifies mail (evidence: `app/MailService.php`)
- Mailbox aggregation → Mailbox registry: selects accounts (evidence: `app/MultiMailbox.php`)
- Mailbox aggregation → IMAP mail service: dispatches sync (evidence: `app/MultiMailbox.php`)
- Mailbox aggregation → SQLite index: reads status (evidence: `app/MultiMailbox.php`)
- Workspace user → Mail endpoints: requests actions (evidence: `README.md`)
- Mail endpoints → IMAP mail service: invokes mail actions (evidence: `api/message.php`)
- Mail endpoints → Mailbox aggregation: requests status (evidence: `api/status.php`)
- Mail endpoints → SQLite index: reads and writes (evidence: `api/inbox.php`)
- Workspace user → Alias management: creates aliases (evidence: `README.md`)
- Alias management → Mailbox registry: maps domains (evidence: `aliases.php`)
- Alias endpoint → Mailbox registry: reads accounts (evidence: `api/aliases.php`)
- Alias endpoint → SQLite index: reads aliases (evidence: `api/aliases.php`)
- Workspace user → Workspace settings: configures workspace (evidence: `README.md`)
- Workspace settings → Settings service: validates settings (evidence: `settings.php`)
- Workspace settings → Mailbox registry: manages mailboxes (evidence: `settings.php`)
- Workspace user → Setup wizard: installs workspace (evidence: `README.md`)
- Setup wizard → Setup service: runs setup (evidence: `setup.php`)
- Setup service → SQLite index: initializes data (evidence: `app/SetupService.php`)
- Workspace user → Backup and migration: migrates settings (evidence: `README.md`)
- Backup and migration → Backup service: exports and imports (evidence: `backup.php`)
- Backup service → SQLite index: snapshots state (evidence: `app/BackupService.php`)
- Backup service → Mailbox registry: snapshots accounts (evidence: `app/BackupService.php`)
- Workspace user → Device sessions: manages devices (evidence: `README.md`)
- Device sessions → Sessions view: renders sessions (evidence: `sessions.php`)
- Sessions view → Session registry: lists sessions (evidence: `app/SessionsView.php`)
- Authentication → Session registry: validates session (evidence: `app/Auth.php`)
- Authentication → SQLite index: checks credentials (evidence: `app/Auth.php`)
- Authentication → Security utilities: returns API errors (evidence: `app/Auth.php`)
- Workspace user → Login challenge: completes challenge
- Application bootstrap → SQLite index: initializes schema (evidence: `bootstrap.php`)
- Application bootstrap → Mailbox registry: initializes registry (evidence: `bootstrap.php`)
- Application bootstrap → Authentication: loads authentication (evidence: `bootstrap.php`)
- Application bootstrap → Shared layout: loads layout (evidence: `bootstrap.php`)
- SQLite index → SQLite storage: persists index (evidence: `app/Database.php`)
- Inbox and reader → Navigation: renders navigation (evidence: `index.php`)
