【Mail Server 系列文:第 9 篇】
在前面幾篇我們完成了:
- Postfix(SMTP)
- Dovecot(IMAP/POP3)
- Amavis / ClamAV / SpamAssassin(安全防護)
- MariaDB / PostfixAdmin(帳號與領域管理)
- Piler(郵件封存系統)
- Manticore(支援中文全文檢索)
接著,本篇將介紹:
如何在 Docker 中部署 Roundcube Webmail,並完整整合:
- ✔ IMAP over TLS
- ✔ SMTP Submission over TLS
- ✔ Dovecot ManageSieve(郵件規則)
- ✔ 與反向代理(Apache/Nginx)結合
- ✔ 日誌、資料庫、插件等配置
Roundcube 是企業最常用的 Webmail 方案,外觀現代、支援插件、擴充容易。本篇將從部署到整合全面說明。
1. Webmail 的角色與必要性
為什麼需要 Webmail?
| 使用情境 | 說明 |
|---|---|
| 使用者出差 | 不方便使用 Outlook、Thunderbird,需透過瀏覽器查看郵件 |
| 行動裝置 | 不依賴手機原生 App,直接登入 Web 即可 |
| 企業內部 | 測試帳號、寄送郵件、檢查 IMAP/SMTP |
| 教育或小型企業 | 無需安裝客戶端,降低維護成本 |
| 客製化需求 | 自訂品牌、加入內部 SSO、插件擴充 |
Roundcube 是 PHP + Apache/NGINX 的成熟 Webmail,支援:
- IMAP / SMTP
- TLS/SSL
- Sieve 郵件規則(透過 ManageSieve)
- 插件:日曆、簽名管理、多語系等
2. 建立 Roundcube 所需目錄
mkdir -p /opt/docker/mail/roundcube/config
mkdir -p /opt/docker/mail/roundcube/db
mkdir -p /opt/docker/mail/roundcube/log
chown -Rf 33:33 /opt/docker/mail/roundcube/db
chown -Rf 33:33 /opt/docker/mail/roundcube/log
- Roundcube image 使用
www-data(UID 33) - SQLite DB 與 log 必須具備寫入權限
3. 拉取官方 Docker Image
本系列使用官方 non-root 版本:
docker pull roundcube/roundcubemail:1.6.11-apache-nonroot
4. 建立啟動容器的 script(webmail.sh)
docker run -dit --name webmail \
--restart=always \
--network intranet-net \
--add-host it.demo.tw:172.18.0.1 \
-e TZ=Asia/Taipei \
-e ROUNDCUBEMAIL_DEFAULT_HOST=tls://it.demo.tw \
-e ROUNDCUBEMAIL_DEFAULT_PORT=143 \
-e ROUNDCUBEMAIL_SMTP_SERVER=tls://it.demo.tw \
-e ROUNDCUBEMAIL_SMTP_PORT=587 \
-e ROUNDCUBEMAIL_DB_TYPE=sqlite \
-e ROUNDCUBEMAIL_USERNAME_DOMAIN=it.demo.tw \
-e ROUNDCUBEMAIL_PLUGINS=managesieve \
-v $PWD/config:/var/roundcube/config \
-v $PWD/db:/var/roundcube/db \
-v $PWD/config/managesieve.config.inc.php:/var/roundcube/plugins/managesieve/config.inc.php \
roundcube/roundcubemail:1.6.11-apache-nonroot
Roundcube 的 IMAP/SMTP 皆透過 TLS:
| 類型 | 協定 |
|---|---|
| IMAP | tls://it.demo.tw port 143 (STARTTLS) |
| SMTP | tls://it.demo.tw port 587(SUBMISSION + STARTTLS) |
5. Roundcube 主設定檔:config.inc.php
檔案位置:
/opt/docker/mail/roundcube/config/config.inc.php
內容:
date_default_timezone_set('Asia/Taipei');
$config['default_host'] = 'tls://it.demo.tw';
$config['default_port'] = 143;
$config['imap_conn_options'] = [
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
],
];
$config['smtp_server'] = 'tls://it.demo.tw';
$config['smtp_port'] = 587;
$config['smtp_user'] = '%u';
$config['smtp_pass'] = '%p';
$config['smtp_conn_options'] = [
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
],
];
$config['smtp_helo_host'] = 'webmail.it.demo.tw';
$config['smtp_log'] = true;
$config['log_driver'] = 'file';
$config['log_dir'] = '/var/log/webmail';
$config['debug_level'] = 4;
6. ManageSieve 插件(郵件規則管理)
位置:
/opt/docker/mail/roundcube/config/managesieve.config.inc.php
內容:
<?php
$config['managesieve_host'] = 'tls://dovecot';
$config['managesieve_port'] = 4190;
$config['managesieve_usetls'] = false;
$config['managesieve_conn_options'] = [
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
'peer_name' => 'it.demo.tw',
'cafile' => '/etc/ssl/certs/ca-certificates.crt',
],
];
Roundcube 可直接管理 Sieve rules,例如:
- 自動分類郵件
- 自動搬移垃圾郵件
- 特定寄件者→指定資料夾
- 自動回覆(Vacation Plugin)
7. 反向代理設定(Apache)
Webmail 透過 HTTPS 對外提供服務:
(1) HTTP → HTTPS redirect
<VirtualHost *:80>
ServerName webmail.it.demo.tw
RewriteEngine on
RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
(2) HTTPS + Reverse Proxy
<VirtualHost *:443>
ServerName webmail.it.demo.tw
ProxyPass / http://webmail:8000/
ProxyPassReverse / http://webmail:8000/
SSLCertificateFile /etc/letsencrypt/live/webmail.it.demo.tw/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/webmail.it.demo.tw/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
此設定讓:
- Apache 處理 SSL
- 後端 Roundcube 容器只收 HTTP
- 更易於集中管理憑證
8. 常見問題排查
✔ 無法登入,IMAP 連線失敗
原因:防火牆阻擋容器 → 主機 TCP 143/993
手動加一條(例):
iptables -t filter -I DOCKER-USER 1 -s 172.18.0.0/16 -d 172.18.0.1 -p tcp -j ACCEPT
✔ SMTP 寄信錯誤(AUTH Failed)
檢查 postfix master.cf:
submission inet ...
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_path=inet:dovecot:12345
✔ Sieve 規則無法套用
檢查:
- Dovecot 是否有啟用 ManageSieve
- Roundcube plugin 是否啟用 managesieve
- TLS 設定是否與 Dovecot 相符
9. Roundcube 安裝完成可以做什麼?
使用者可:
- ✔ 收信 / 寄信(IMAP / SMTP / TLS)
- ✔ 管理郵件分類
- ✔ 設定 Sieve 自動規則
- ✔ 建立簽名檔
- ✔ 搜尋郵件
- ✔ 使用行動裝置登入(瀏覽器)
系統管理員可:
- 以 Webmail 測試 Postfix / Dovecot 是否正常
- 用於 demo、教育訓練、測試 IMAP/SMTP 認證
10. 本篇結語
本篇成功完成:
✔ Roundcube Webmail 在 Docker 中部署
✔ TLS → Dovecot / Postfix 整合
✔ ManageSieve 郵件規則管理
✔ 反向代理與 SSL 憑證整合
✔ 常見問題排查
至此,整個郵件系統已具備:
- 完整收發機制
- 安全防護
- Webmail 操作介面
- 郵件規則(Sieve)
- 封存與全文檢索(Piler + Manticore)