How postalserver/postal works
Project Type and Purpose
• This is Postal: a self-hosted, full-featured mail server written in Ruby on Rails. It provides:
• Web UI and HTTP APIs for managing domains, credentials, routes, servers, webhooks, etc.
• An SMTP server to accept incoming mail.
• SMTP client functionality to deliver outbound mail.
• Background workers and scheduled tasks for queue processing, webhooks, cleanup, stats.
• A specialized message-database layer (lib/postal/message_db) for high-volume message storage and live stats.
• Integrations with DNS, DKIM signing, spam-inspectors (ClamAV, SpamAssassin, Rspamd), Prometheus metrics, and external webhook targets.
A. User & API Clients
• Web browsers or CLI tools hit the Rails Web UI and HTTP API (app/controllers, config/routes.rb).
• Legacy API endpoints under /legacy_api.
B. Rails Application
• Controllers → Services/Models → Primary SQL Database (config/database.yml).
• Uses ActiveRecord for configuration data (domains, credentials, users, routes, etc.).
• Hosts an embedded health HTTP server for metrics (app/util/health_server.rb).
C. Message Database
• Separate message‐DB library (lib/postal/message_db) with its own schema optimized for messages, deliveries, clicks, suppressions, webhooks.
• Used by both the Rails app and background workers for message storage and live stats.
D. SMTP Server
• Custom SMTP server implementation (app/lib/smtp_server) listens for SMTP traffic from external SMTP clients.
• Performs SMTP handshake, start‐tls, authentication, and hands off messages to the queue.
E. SMTP Client
• Custom SMTP client (app/lib/smtp_client) used by the delivery subsystem to send mail out to remote mail servers.
F. HTTP Sender
• HTTP‐based endpoints (app/lib/senders/http_sender) for delivering inbound mail via webhooks.
G. Message Queue & Workers
• In‐process queue or external queue (depending on deployment) managed by message_dequeuer (app/lib/message_dequeuer) and background workers (app/lib/worker).
• Worker jobs: process_queued_messages_job, process_webhook_requests_job.
• Scheduled tasks (app/scheduled_tasks) for DNS checks, retention, cleanup, stats, notifications.
H. External Integrations
• DNS Resolver (app/lib/dns_resolver) for domain verification and routing lookups.
• DKIM signer (app/lib/dkim_header & lib/postal/signer).
• Spam inspections via plug-ins in lib/postal/message_inspectors.
• Webhook delivery to user‐configured URLs.
• Prometheus metrics endpoint.
I. Persistence & Configuration
• Primary relational database (PostgreSQL/MySQL) for Rails data.
• File-based config (YAML, ENV vars), Docker & Helm manifests, and Kubernetes configuration exporters.
Component Relationships and Data Flows
1. Administrator & Users
–> Web UI / HTTP API requests reach Rails controllers → business logic → primary DB.
2. Inbound Mail Flow
SMTP client (external sender)
–> SMTP Server → SMTP handshake → message passed to message_dequeuer → message stored in message_db → worker jobs process the message → optionally forwarded via HTTP Sender or delivered to local mailboxes.
3. Outbound Mail Flow
Rails app or API clients enqueue outgoing messages → message_dequeuer stores them → workers pick up → DKIM signing & spam checks → SMTP Client connects to remote MX records → delivers mail.
4. Webhook Delivery
After processing, webhook events enqueued → worker → HTTP Sender → external webhook endpoint.
5. Scheduled Tasks & Metrics
Cron‐like scheduled tasks hit the database, prune old data, check DNS, send notifications → Prometheus metrics scraped from health server.
Architectural Patterns & Design Principles
• Monolithic Rails core with clear sub‐systems (SMTP server, SMTP client, HTTP sender, background workers).
• Separation of concerns: config data vs. high‐volume message data in a specialized message DB library.
• Plugin architecture for message inspection (spam, DKIM).
• Composer pattern for services (app/services, app/senders, app/lib/message_dequeuer).
• Convention over configuration for Rails plus explicit Docker/Kubernetes deployment layers.