1# backend
2
3The `backend` package defines a unified `Provider` interface for multi-protocol email support and provides three protocol implementations: IMAP, JMAP, and POP3.
4
5## Architecture
6
7This package acts as an abstraction layer, allowing the rest of the application to interact with mail servers through a consistent interface regardless of the underlying protocol. Implementations self-register at init time via `RegisterBackend`, and the factory function `New()` creates the right provider based on the account's `Protocol` field (defaults to `"imap"`).
8
9### Provider interface
10
11The `Provider` interface composes five sub-interfaces:
12
13| Interface | Methods | Purpose |
14|-----------|---------|---------|
15| `EmailReader` | `FetchEmails`, `FetchEmailBody`, `FetchAttachment` | Retrieve email lists, bodies, and raw attachments |
16| `EmailWriter` | `MarkAsRead`, `DeleteEmail`, `ArchiveEmail`, `MoveEmail` | Modify email state and location |
17| `EmailSender` | `SendEmail` | Send outgoing mail |
18| `FolderManager` | `FetchFolders` | List available mailboxes |
19| `Notifier` | `Watch` | Real-time push notifications for mailbox changes |
20
21Backends that don't support an operation return `ErrNotSupported`.
22
23## Protocols
24
25### IMAP (`backend/imap`)
26
27Wraps the existing `fetcher` and `sender` packages behind the `Provider` interface. IMAP IDLE is handled externally in `main.go`, so `Watch()` returns `ErrNotSupported`.
28
29### JMAP (`backend/jmap`)
30
31Native JMAP implementation (RFC 8620 / RFC 8621) using `go-jmap`. Supports OAuth2 and Basic Auth, real-time push via JMAP EventSource, and full mailbox operations including send (via `EmailSubmission`). JMAP string IDs are hashed to `uint32` UIDs for interface compatibility.
32
33### POP3 (`backend/pop3`)
34
35POP3 + SMTP implementation. Inherently limited to a single INBOX folder, no read flags, no move/archive, and no push notifications. Uses the `sender` package for outgoing mail.
36
37## Files
38
39| File | Description |
40|------|-------------|
41| `backend.go` | Core interfaces and data types (`Provider`, `Email`, `Attachment`, `Folder`, `OutgoingEmail`, `NotifyEvent`, `Capabilities`) |
42| `factory.go` | Protocol registry and `New()` factory function |
43| `imap/imap.go` | IMAP provider — adapter over `fetcher` and `sender` packages |
44| `jmap/jmap.go` | JMAP provider — native implementation with session management and mailbox caching |
45| `pop3/pop3.go` | POP3 provider — per-connection model with UIDL-based UID hashing |