Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

Dovecot:IMAP / POP3 / LMTP / SQL / SNI / Quota / Sieve / Spam 學習整合

Posted on 2025-11-202025-11-21 by Rico

【Mail Server 系列文:第 5 篇】

在上一章(第 4 篇)中,我們完成了 Postfix SMTP 核心:

  • 支援 MySQL 虛擬網域
  • 支援 SNI TLS
  • 以 LMTP 將信件交給 Dovecot
  • 通過 Amavis、Piler 進行過濾與封存
  • submission 587/SMTPS 465 完整登入流程

本篇則要處理 Postfix 後端最關鍵的角色:Dovecot。

Dovecot 負責真實的信件儲存、認證、收信協定、Sieve、自動 Spam 學習等工作,是整個 Mail 系統運作品質的關鍵。


🧩 1. 本篇完成後你將擁有

✔ IMAP(143/993)、POP3(110/995)、LMTP(24) 完整收信功能
✔ Dovecot SQL 驗證(與 PostfixAdmin 資料庫整合)
✔ 支援 SNI 多網域 TLS 憑證
✔ 每個使用者的 mailbox quota
✔ quota-warning 即時通知信
✔ Sieve 過濾規則(含垃圾郵件學習)
✔ IMAPSieve(使用 Roundcube 可實現「移動郵件 → 自動學習 Spam/Ham」)
✔ SpamAssassin remote-learn 整合
✔ 完整 Docker 化服務


📦 2. 建立 Dovecot 的 Docker Image

使用 Alpine 3.22.1 作為基底,並加入:

  • dovecot
  • dovecot-mysql
  • dovecot-pigeonhole-plugin(提供 sieve / managesieve)
  • spamassassin-client(提供 spamc)

Dockerfile

FROM alpine:3.22.1

RUN apk update && \
    apk add --no-cache \
    dovecot dovecot-lmtpd dovecot-mysql dovecot-pigeonhole-plugin \
    bash vim tzdata ca-certificates \
    spamassassin-client \
    shadow su-exec

RUN addgroup -g 5000 -S vmail && \
    adduser -D -S -H -s /sbin/nologin -u 5000 -G vmail vmail

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 110 995 143 993 24 4190
CMD ["/entrypoint.sh"]

🚀 3. Dovecot 的目錄結構

/opt/docker/mail/dovecot/
 ├─ config/          → /etc/dovecot
 ├─ sieve/           → 全域 + before/after rules
 ├─ shell/           → quota warning / spam learn scripts
 ├─ log/             → /var/log/dovecot
 └─ usermail/        → /var/vmail 實際信箱

設定 vmail 權限:

chown -Rf 5000:5000 /opt/docker/mail/dovecot/usermail
chown -Rf 5000:5000 /opt/docker/mail/dovecot/config/sieve

🧩 4. SQL 認證(MySQL 與 PostfixAdmin 一致)

Dovecot 認證來源:

mysql → postfixadmin 資料庫 → mailbox 表

你將在 /etc/dovecot/dovecot-sql.conf.ext 指定:

driver = mysql
connect = host=maildb dbname=postfix user=postfix password=postfix1230

password_query = SELECT username AS user, password FROM mailbox WHERE username='%u' AND active='1'
user_query = SELECT 5000 AS uid, 5000 AS gid, '/var/vmail/%d/%n' AS home, 'maildir:/var/vmail/%d/%n' AS mail

這樣才能與 PostfixAdmin 一致運作,並讓 Roundcube 能成功登入。


🔐 5. SNI TLS:多網域 SSL 支援

Postfix 需要 SNI,而 Dovecot 同樣需要。

在 10-ssl.conf:

ssl = required
ssl_cert = </etc/letsencrypt/live/it.demo.tw/fullchain.pem
ssl_key  = </etc/letsencrypt/live/it.demo.tw/privkey.pem

local_name it.demo.tw {
  ssl_cert = </etc/letsencrypt/live/it.demo.tw/fullchain.pem
  ssl_key  = </etc/letsencrypt/live/it.demo.tw/privkey.pem
}

local_name nuface.tw {
  ssl_cert = </etc/letsencrypt/live/nuface.tw/fullchain.pem
  ssl_key  = </etc/letsencrypt/live/nuface.tw/privkey.pem
}

📥 6. LMTP – Postfix → Dovecot 投遞

Postfix 第 4 篇中:

virtual_transport = lmtp:inet:dovecot:24

Dovecot 端:

10-master.conf:

service lmtp {
  unix_listener lmtp {
    mode = 0660
    group = postfix
    user = postfix
  }
  inet_listener lmtp {
    address = *
    port = 24
  }
}

LMTP 比 local delivery 更安全、支援 quota、錯誤回報更完整。


📦 7. Quota 設定與 quota-warning 通知

Dovecot quota:

plugin {
  quota = maildir:User quota
  quota_rule = *:storage=2G
  quota_rule2 = Trash:storage=+100M
}

套用通知 shell:

plugin {
  quota_warning = storage=80%% quota-warning 80 %u
  quota_warning2 = storage=95%% quota-warning 95 %u
  quota_warning3 = -storage=100%% quota-warning below %u
}

執行腳本(你已寫好):

  • quota-warning.sh
    (寄信給使用者提醒剩餘空間)

🧠 8. SpamAssassin Remote Learn(使用 IMAPSieve)

本架構支援:

  • 移動到垃圾桶 → 學習 spam
  • 從垃圾桶移出 → 學習 ham

Roundcube 也能直接支援 learning!

Dovecot Sieve:

global/learn-spam.sieve
global/learn-ham.sieve

兩個 shell script:

sa-remote-learn-spam.sh
sa-remote-learn-ham.sh

透過:

spamc -d spamassassin -p 783 -u <user> -L spam
spamc -d spamassassin -p 783 -u <user> -L ham

即可將內容提交至 SpamAssassin SQL Bayes。


🧮 9. Sieve / ManageSieve(Roundcube Webmail 過濾規則)

啟用 ManageSieve(讓 Roundcube 可編輯規則)

protocol sieve {
  listen = *:4190
}

Roundcube 使用:

tls://dovecot:4190

🐳 10. 啟動 Dovecot 容器

docker run -dit --name dovecot \
  --restart=always \
  --network intranet-net \
  -p 110:110 -p 995:995 \
  -p 143:143 -p 993:993 \
  -p 24:24 -p 4190:4190 \
  -e TZ=Asia/Taipei \
  -v $PWD/config:/etc/dovecot \
  -v $PWD/log:/var/log \
  -v $PWD/usermail:/var/vmail \
  -v /opt/docker/wwwapp/data/etc-letsencrypt:/etc/letsencrypt \
  nuface/dovecot-2.4.1-4:1.0

🧪 11. 測試你的 Dovecot 是否正常

1. 檢查版本

dovecot --version

2. 測試 IMAPS

openssl s_client -connect it.demo.tw:993

3. 測試 LMTP 是否可投遞

telnet dovecot 24

4. 測試 SQL 驗證

doveadm auth test [email protected] 密碼

5. 測試 Sieve 是否生效

doveadm sieve list -u [email protected]

6. 測試垃圾郵件學習

cat sample.eml | spamc -L spam -u [email protected]

🏁 12. 本篇總結:你的收信系統已完工

在本篇中,你已完成:

✔ 完整 IMAP/POP3/LMTP 收信系統
✔ SQL 認證整合
✔ 完整 SNI TLS 多網域
✔ Sieve + ManageSieve 完整郵件規則
✔ 即時 Quota 通知
✔ 自動 Spam/Ham 學習
✔ 完整 Docker 化佈署

Dovecot 是郵件系統「最終信箱」的核心,而你現在已擁有一套企業等級、可多網域、多使用者、具備完整 Spam 學習的收信架構。

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme