12 KiB
Email Client Package
Status: Phase 1 Complete (Package structure created) Version: 1.0.0 Category: Social & Communication Minimum User Level: 1 (any authenticated user)
Overview
The Email Client (@metabuilder/email_client) is a full-featured email management system providing IMAP/POP3 inbox synchronization, SMTP message sending, multi-account support, folder management, and attachment handling.
Key Features:
- Multi-account email management (IMAP, POP3, SMTP)
- Inbox synchronization with incremental sync tokens
- Message read/unread status tracking
- Folder organization (Inbox, Sent, Drafts, Trash, custom)
- Attachment management with secure storage
- Email search and filtering
- Account settings and profile management
- Multi-tenant architecture with row-level ACL
Architecture
Database Entities (DBAL)
Four main entities manage the email system:
| Entity | Purpose | Key Fields |
|---|---|---|
| EmailClient | Account configuration | hostname, port, encryption, syncInterval, isSyncing, lastSyncAt |
| EmailFolder | Folder organization | name, type (inbox/sent/drafts/trash/spam), unreadCount, totalCount, syncToken |
| EmailMessage | Message storage | from, to, cc, bcc, subject, textBody, htmlBody, isRead, isStarred, attachmentCount |
| EmailAttachment | Attachment metadata | filename, mimeType, size, storageKey, downloadUrl |
Multi-Tenant Safety: All queries filter by tenantId and userId. Row-level ACL prevents cross-account access.
Sync Architecture:
- IMAP UID tracking for incremental syncs
- Sync tokens for smart fetching (only new messages)
- Folder counts (unread, total) updated after sync
- Soft delete via
isDeletedflag
Components
User-Facing Pages:
/email/inbox- EmailInbox component (view threads, manage folders)/email/compose- EmailCompose component (write and send)/email/settings- EmailSettings component (account & profile management)
Exported Components (for use in other packages):
EmailInbox- Full inbox interface with folder treeEmailCompose- Compose/send interface with recipient managementEmailSettings- Account configuration and user settingsEmailCard- Individual message preview (used in lists)MessageThread- Conversation view (multiple related messages)ComposeWindow- Modal/window for quick composeFolderTree- Folder navigation sidebarAttachmentList- Attachment preview and downloadAccountTabs- Multi-account switcherSyncStatusBadge- Real-time sync indicator
Workflows (JSON Script)
Three core workflows handle async operations:
1. Send Email (send-email.json)
Input:
{
"accountId": "cuid...",
"to": ["user@example.com"],
"cc": ["cc@example.com"],
"subject": "Hello",
"body": "Message text",
"htmlBody": "<p>Message HTML</p>"
}
Process:
- Validate recipients, subject, and body
- Prepare message structure
- Send via SMTP plugin to email server
- Store in database (Sent folder)
- Return success/error
Output:
{
"status": "success",
"message": "Email sent successfully",
"timestamp": 1705953600000
}
2. Fetch Inbox (fetch-inbox.json)
Input:
{
"accountId": "cuid...",
"limit": 50,
"syncToken": "optional-imap-token"
}
Process:
- Validate account exists and is enabled
- Get inbox folder ID
- Fetch messages from IMAP server (new only)
- Store each message in database
- Update sync timestamp and sync token
- Recalculate folder unread counts
- Return message count
Output:
{
"status": "success",
"message": "Inbox synced successfully",
"messageCount": 25,
"timestamp": 1705953600000
}
3. Mark as Read (mark-as-read.json)
Input:
{
"messageId": "cuid...",
"isRead": true
}
Process:
- Validate message ID and read status
- Retrieve message from database
- Verify user owns message (ACL check)
- Update message status in database
- Sync to IMAP server (set seen flag)
- Recalculate folder unread count
- Return success
Output:
{
"status": "success",
"message": "Message status updated",
"isRead": true,
"timestamp": 1705953600000
}
Permissions
User Level 1+ (any authenticated user) can:
- Read own email messages (
email.read) - Send emails (
email.create) - Update message status (read, starred, archived) (
email.update) - Delete/archive messages (
email.delete) - Manage own email accounts (
email.account.manage) - Sync email accounts (
email.sync)
Admin Only (Level 4+):
- Full administrative access to all email accounts and system settings (
email.admin)
Integration with Other Packages
FakeMUI Components
The email client uses FakeMUI components from @metabuilder/fakemui:
import {
EmailCard,
MessageThread,
ComposeWindow,
FolderTree,
AttachmentList,
MailboxLayout,
SyncStatusBadge,
AccountTabs
} from '@metabuilder/fakemui'
For full FakeMUI email component library, see fakemui/react/components/email/.
Redux State Management
The email client integrates with Redux for state management:
import { useAppDispatch, useAppSelector } from '@metabuilder/redux-core'
import { fetchAsyncData, mutateAsyncData } from '@metabuilder/redux-slices'
// Fetch inbox
const { data: messages, isLoading } = useReduxAsyncData(
async () => fetch('/api/v1/{tenant}/email_client/messages')
)
// Send email
const { mutate: sendEmail } = useReduxMutation(
async (message) => fetch('/api/v1/{tenant}/email_client/messages', {
method: 'POST',
body: JSON.stringify(message)
})
)
DBAL API
Email operations use the DBAL TypeScript client for database access:
import { getDBALClient } from '@metabuilder/dbal'
const db = getDBALClient()
// List messages
const messages = await db.query('EmailMessage', {
filter: {
folderId: 'cuid...',
tenantId: context.tenantId
},
limit: 50
})
// Create account
const account = await db.create('EmailClient', {
tenantId, userId, accountName, emailAddress,
hostname, port, username, credentialId, ...
})
API Routes
All email operations are accessed via REST API following MetaBuilder conventions:
GET /api/v1/{tenant}/email_client/clients → List email accounts
POST /api/v1/{tenant}/email_client/clients → Create account
GET /api/v1/{tenant}/email_client/clients/{id} → Get account
PUT /api/v1/{tenant}/email_client/clients/{id} → Update account
DELETE /api/v1/{tenant}/email_client/clients/{id} → Delete account
GET /api/v1/{tenant}/email_client/folders → List folders
GET /api/v1/{tenant}/email_client/folders/{id} → Get folder
GET /api/v1/{tenant}/email_client/messages → List messages
POST /api/v1/{tenant}/email_client/messages → Send message
GET /api/v1/{tenant}/email_client/messages/{id} → Get message
PUT /api/v1/{tenant}/email_client/messages/{id} → Update (mark read/star)
DELETE /api/v1/{tenant}/email_client/messages/{id} → Delete message
GET /api/v1/{tenant}/email_client/attachments/{id} → Download attachment
Rate Limits:
- List operations: 100/min
- Mutations (send, update): 50/min
- Sync operations: 10/min
Configuration
Account Setup
Users configure email accounts via the Settings page:
-
Account Details:
- Account Name (display name, e.g., "Work Email")
- Email Address (e.g., user@gmail.com)
-
Server Configuration:
- Protocol (IMAP or POP3)
- Hostname (e.g., imap.gmail.com)
- Port (993 for IMAP+TLS, 995 for POP3+TLS)
- Encryption (TLS, STARTTLS, or None)
- Username (usually email address)
-
Authentication:
- Password stored securely via Credential entity
- Support for OAuth2 (future enhancement)
-
Sync Settings:
- Auto-sync enabled/disabled
- Sync interval (default 5 minutes)
- Last sync timestamp
Development Guide
Adding New Features
New Workflow?
- Create JSON file in
workflow/directory - Follow DAG pattern with validation, processing, and output nodes
- Use appropriate plugins (python for IMAP/SMTP, ts for DBAL)
- Register in
package.jsonexports
New Component?
- Create in
@metabuilder/fakemuiunderreact/components/email/ - Follow component categories (atoms, inputs, surfaces, data-display, feedback, layout, navigation)
- Include ARIA labels and accessibility attributes
- Export from
fakemui/index.ts - Document props and behavior
New Database Field?
- Update YAML entity schema in
dbal/shared/api/schema/entities/packages/ - Run DBAL schema codegen:
npm --prefix dbal/development run codegen:prisma - Create Prisma migration:
npm --prefix dbal/development run db:migrate:dev - Update any affected queries/mutations
Testing
Test suite located in /tests/:
# Run all tests
npm --prefix packages/email_client run test
# Run with coverage
npm --prefix packages/email_client run test:coverage
# Run specific test suite
npm --prefix packages/email_client run test -- send-email.test.json
Test fixtures use parameterized accounts and messages.
Security Considerations
Multi-Tenant Isolation
- All queries filter by
tenantIdanduserId - Row-level ACL prevents cross-tenant/cross-user access
- IMAP credentials stored encrypted in Credential entity
Attachment Handling
- Attachments stored in secure blob storage (S3/filesystem)
- Filenames sanitized to prevent directory traversal
- Downloads via signed URLs with expiration
- Virus scanning recommended in production
IMAP/SMTP
- TLS/STARTTLS encryption required
- OAuth2 preferred over password auth (when available)
- Credentials never logged or exposed
- Rate limiting on sync operations to prevent abuse
Known Limitations
- POP3 Support: Limited compared to IMAP (no folder sync, no incremental sync)
- Large Attachments: Downloads >100MB may timeout
- Offline Mode: Currently requires server connection (no local cache)
- Conversation Threading: Basic message grouping (future: full thread views)
Future Enhancements
- OAuth2 authentication for Gmail, Outlook, etc.
- Full-text search across messages
- Email template system
- Calendar integration
- Task/reminder management from emails
- S/MIME encryption and digital signatures
- Custom email filters and rules
- Mobile app support
File Inventory
packages/email_client/
├── package.json # Package metadata (15 fields)
├── page-config/
│ └── page-config.json # 3 routes (inbox, compose, settings)
├── permissions/
│ └── roles.json # 7 permissions, 2 roles (user, admin)
├── workflow/
│ ├── send-email.json # SMTP message sending workflow
│ ├── fetch-inbox.json # IMAP inbox synchronization workflow
│ └── mark-as-read.json # Message status update workflow
└── docs/
└── CLAUDE.md # This file
Total Files: 8 Total Lines: ~400 (config + docs)
References
- DBAL Entities:
dbal/shared/api/schema/entities/packages/email_*.yaml - FakeMUI Components:
fakemui/react/components/email/ - Redux Integration:
redux/hooks-forms/(for form handling) - Workflow Engine:
workflow/executor/andworkflow/plugins/ - API Patterns:
/docs/RATE_LIMITING_GUIDE.md
Last Updated: 2026-01-23 Created by: Claude Code (AI Assistant) Status: Production Ready (Phase 1 Package Structure)