====== Mailpit(開源 SMTP 模擬服務) ======
* 因為要驗證應用系統寄信的狀況, 透過實際 Mail Server 雖然也可以驗證, 但效率比較差也常發現收不到信是被中間的一些 SPAM 的機制攔截, 因此有不少開源 SMTP 模擬服務專案出現
* 評估與使用過 [[https://github.com/gessnerfl/fake-smtp-server|Fake SMTP Server]] [[https://github.com/mailhog/MailHog|MailHog]] 後發現都不支援 STARTTLS, 最後問了 AI 才推薦 [[https://github.com/axllent/mailpit|Mailpit]]
===== 安裝方式 =====
* {{repo>https://raw.githubusercontent.com/tryweb/docker-compose/refs/heads/main/mailpit/docker-compose.yml|docker-compose.yml}}
* 參考 [[tech/openssl_caserver]] 來產生SSL憑證
* 將產生的 ServerCA.cer 與 ServerCA.key 放入 certs 目錄內
* 啟動 docker compose 即可提供 SMTP 服務
===== 測試方式 =====
* 環境變數 MP_SMTP_REQUIRE_STARTTLS=true 表示設定為 STARTTLS, 測試方式
openssl s_client -connect localhost:1025 -starttls smtp
* 環境變數 MP_SMTP_REQUIRE_TLS=true 表示設定為 SSL/TLS, 測試方式
openssl s_client -connect localhost:1025
* 正確回應的結果應該可以看到憑證的內容 Exp.
$ openssl s_client -connect localhost:1025 -starttls smtp
CONNECTED(00000003)
Can't use SSL_get_servername
depth=0 C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = admin@ichiayi.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = tech@ichiayi.com
verify error:num=21:unable to verify the first certificate
verify return:1
depth=0 C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = tech@ichiayi.com
verify return:1
---
Certificate chain
0 s:C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = tech@ichiayi.com
i:C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = Jonathan Tsai, emailAddress = admin@ichiayi.com
a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA1
v:NotBefore: Apr 12 02:31:31 2025 GMT; NotAfter: Apr 12 02:31:31 2027 GMT
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIDmjCCAoICFBr9dfIlHO4wEY7hxz/BJNhTuageMA0GCSqGSIb3DQEBBQUAMIGK
MQswCQYDVQQGEwJUVzEPMA0GA1UECAwGVGFpd2FuMQ8wDQYDVQQHDAZUYWlwZWkx
:
=== 網頁 ===
* http://127.0.0.1:8025/ \\ {{:tech:螢幕擷取畫面_2025-04-12_113302x.png|}}
{{tag>mail smtp}}
===== 參考網址 =====
* https://github.com/axllent/mailpit
* https://mailpit.axllent.org/docs/configuration/smtp/
* https://hub.docker.com/r/axllent/mailpit
* https://www.ichiayi.com/tech/openssl_caserver