【Mail Server 系列文:第 14 篇】
在前 1–13 篇中,我們已經完成了一整套企業級郵件系統:
Postfix、Dovecot、Amavis、ClamAV、SpamAssassin、Roundcube、Piler、Manticore、MariaDB、Proxy、SSL、SNI…
所有組件都已經成功被容器化、整合並投入實際運作。
本篇是整個系列的收尾篇,將完整呈現 系統架構圖、流量路徑、元件互動與流程圖,並清楚整理整個系統的全貌。
📌 一、Mail Server 系統完整架構圖(總覽)
以下架構圖描述了所有模組與 Docker 架構:
┌───────────────────────────────────────┐
│ Internet │
│ (Sender SMTP / Receiver SMTP/IMAP) │
└───────────────────────────────────────┘
│
▼
┌────────────────────┐
│ Proxy / Firewall │
│ (OPNsense / iptables)│
└────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────┐
│ Docker Host (172.18.0.1) │
│ intranet-net 虛擬網路 │
└────────────────────────────────────────────────────────────┘
│
▼
──────────────────────────────── 郵件主要服務 ────────────────────────────────
Postfix(SMTP)
──────────────
- 收信(25)
- 寄信(587 / 465)
- DKIM / SNI
- SQL 虛擬網域 / 郵件路由(transport)
- Postfix-Milter → Piler(X-Envelope)
Dovecot(IMAP/POP/LMTP/Sieve)
─────────────────────────────
- IMAP/POP3
- LMTP 投遞
- Sieve 過濾規則
- Spam/Ham 遠端學習(SpamAssassin)
- Quota(超限通知)
Amavis → ClamAV + SpamAssassin
─────────────────────────────
- 病毒掃描
- 垃圾信過濾
- DKIM 簽章
- 內容過濾
Piler(Mail Archive)
─────────────────────────────
- Email Store
- Metadata
- Permission Mapping
- Web Portal
Manticore Search(全文檢索)
─────────────────────────────
- CJK 分詞
- Real-time Index
- SQL Search
Roundcube(Webmail)
─────────────────────────────
- Webmail UI
- ManageSieve
- SMTP/IMAP 連線
MariaDB(後端資料庫)
─────────────────────────────
- PostfixAdmin
- Domain & Mailbox
- Alias / Transport
- Piler DB
Apache Proxy(webmail / archive)
───────────────────────────────
- HTTPS
- 反向代理
- Let's Encrypt SSL
Memcached
───────────────────────────────
- Piler Cache
- Session Cache
📌 二、郵件收信流程(Inbound Flow)
Internet → Postfix(25) → content_filter → Amavis → ClamAV/SpamAssassin
→ 回投至 Postfix(10025) → LMTP → Dovecot → 存入 Maildir
→ Piler Milter(X-Envelope-To) → Piler Archive → Manticore Index
步驟解析:
- 外部郵件伺服器透過 SMTP port 25 將郵件送至 Postfix
- Postfix 將信交給 Amavis 做內容過濾
- Amavis 呼叫:
- ClamAV(病毒掃描)
- SpamAssassin(垃圾信評分)
- DKIM/Anti-Spam Header 注入
- Amavis 將信回送至 Postfix(port 10025)
- Postfix 計算收件人 → 使用 LMTP 投遞至 Dovecot
- Dovecot 寫入 maildir
- Piler Milter 在 SMTP 阶段寫入 Envelope Header
- Piler 接收郵件(由 always_bcc)→ 存檔 → Manticore 索引
- 郵件完成入庫 + 入信箱
📌 三、郵件寄信流程(Outbound Flow)
Roundcube / Outlook / Client → Submission(587)/SMTPS(465)
→ SASL(Dovecot Auth) → Postfix → Amavis(10026) → DKIM Sign
→ Postfix Relay → Internet
步驟解析:
- 使用者登入 Roundcube / Outlook
- Client 使用:
- 587 (STARTTLS)
- 465 (SSL wrapper)
- Postfix 透過 Dovecot SASL 驗證使用者
- 信件送入 Amavis(10026)埠:
- DKIM 簽章
- Anti-Spam Header 設定
- Postfix Relay → Internet
Outbound 的安全辯證:
- SASL 強制認證
- TLS Encryption
- 避免寄件者假冒(reject_authenticated_sender_login_mismatch)
- milter_macro_daemon_name=ORIGINATING 區分內外部來源
📌 四、Piler(郵件歸檔)與 Manticore(全文檢索)流程
Piler 主要任務:
✔ 長期信件保存
✔ Metadata + Envelope Mapping
✔ 分群權限(誰能看哪些信)
✔ 提供 Web 介面搜尋/預覽
✔ IMAP Restore(復原至 Dovecot)
Manticore 主要任務:
✔ 中文分詞(ngram + ICU)
✔ 即時寫入索引(RT Index)
✔ Full-Text Query(subject / body / address / attachment name)
整體流程
Postfix always_bcc → piler@[local]
↓
Piler SMTP
↓
Metadata → MariaDB (piler)
Raw Mail → Piler Store (/var/piler/store)
↓
Manticore RT Index
↓
User Search → Archive Web UI → SQL Query → Result + Preview
📌 五、Spam/Ham 遠端學習(IMAPSieve → SpamAssassin Remote)
使用者在 Roundcube:
- 將信件拖到垃圾信(SPAM) → sa-learn spam
- 將信件拖回 INBOX → sa-learn ham
架構:
Dovecot IMAPSieve → global/learn-spam.sieve → sa-remote-learn-spam.sh
↳ spamc -L spam
Dovecot IMAPSieve → global/learn-ham.sieve → sa-remote-learn-ham.sh
↳ spamc -L ham
所有學習都透過 spamassassin 容器 完成,使 SpamAssassin Bayes 好好運作。
📌 六、Roundcube Webmail 與 Apache 反向代理
使用方式
瀏覽器 → https://webmail.it.demo.tw/
Apache Proxy:
443 → ProxyPass → http://webmail:8000/
Roundcube → Dovecot:
- TLS IMAP 143(STARTTLS)
- ManageSieve 4190
Roundcube → Postfix:
- SMTP 587(STARTTLS)
📌 七、主機防火牆與 Docker 網路規劃
重要原則:
- 所有容器均位於 intranet-net 172.18.0.0/16
- 主機(172.18.0.1)對容器提供 TLS Proxy
- DOCKER-USER chain 最後要留一個 REJECT
- 所有內部服務必須加:
iptables -I DOCKER-USER 1 -s 172.18.0.0/16 -d 172.18.0.1 -j ACCEPT
以避免 Webmail 的 IMAP/TLS 被擋。
📌 八、所有容器服務總表
| 服務 | Container | Port | 主要功能 |
|---|---|---|---|
| Postfix | postfix | 25 / 587 / 465 | SMTP 收發、SASL、DKIM、SNI |
| Dovecot | dovecot | 110 / 995 / 143 / 993 / 24 / 4190 | IMAP/POP/LMTP/Sieve/Quota |
| Amavis | amavis | 10024 / 10026 | Anti-Spam / Anti-Virus / DKIM |
| ClamAV | clamav | local socket | 病毒掃描 |
| SpamAssassin | spamassassin | 783 | Spam 評分 + Remote Learn |
| MariaDB | maildb | 3306 | PostfixAdmin + Piler DB |
| Piler | piler | 25 / Web UI | 郵件歸檔 |
| Manticore | manticore | 9306 / 9312 | 全文檢索 |
| Roundcube | webmail | 8000 | Webmail |
| Apache Proxy | wwwapp | 80 / 443 | Webmail / Archive Proxy |
| Memcached | memcached | 11211 | Cache |
📌 九、整套 Mail Server 架構完成後的能力
✔ 可支援多網域
✔ 支援 SNI TLS
✔ 支援 DKIM / SPF / DMARC
✔ 支援 Anti-Spam、Anti-Virus
✔ 支援 Webmail
✔ 支援 Mail Archive + Full-Text Search
✔ 支援 Outlook / Thunderbird / Mobile Client
✔ 支援 即時 Spam/Ham 學習
✔ 100% Docker 化,多主機可搬移
✔ 企業級可用性(高彈性 + 高可維護性)
此架構已適用於中小企業甚至大型企業,功能與商用郵件系統相同甚至更強。