CLOVER🍀

That was when it all began.

ローカルでのテストに使えるメールサーバー、MailHogを試す

ちょっと、テストに使えるSMTPサーバーを探していまして。

探してみると、こういうのが見当たります。

FakeSMTP – FakeSMTP - Dummy SMTP server for developers

GitHub - rnwood/smtp4dev: smtp4dev - the mail server for development

Origami SMTP

GitHub - ChangemakerStudios/Papercut: Papercut Simple Desktop SMTP Server

MailCatcher

GitHub - mailhog/MailHog: Web and API based SMTP testing

便利なサービスもあるようですね。

メールのテストに便利なMailTrap - Challenge Java EE !

今回は、ローカルで動かせて、Web UIでメールの確認もできるMailHogを試してみることにしました。

GitHub - mailhog/MailHog: Web and API based SMTP testing

MailCatcherにインスパイアされて作られたものらしいです。

MailCatcher

機能としては、

  • ESTM Server
  • SMTP AUTHとPIPELININGのサポート
  • Web UI付き
  • Failure Testing
  • HTTP API
  • Multipart MIMEのサポート
  • ストレージはメモリ、ファイル、MongoDBをサポート

といった感じで。

ドキュメントは、こちら。

https://github.com/mailhog/MailHog/tree/v1.0.0/docs

インストールは、バイナリをダウンロードして実行権限を付与。
※確認は、Ubuntu Linux 18.04 LTSで行っています

$ wget https://github.com/mailhog/MailHog/releases/download/v1.0.0/MailHog_linux_amd64
$ chmod a+x MailHog_linux_amd64

シングルバイナリなところが良いですね!(実装はGolangです)

あとは実行すれば起動します。

$ ./MailHog_linux_amd64
2018/12/04 14:43:19 Using in-memory storage
2018/12/04 14:43:19 [SMTP] Binding to address: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
2018/12/04 14:43:19 Serving under http://0.0.0.0:8025/
Creating API v1 with WebPath: 
Creating API v2 with WebPath:

バインドするアドレス、ポートを変えたり、メールの保存先を変えたりできます。

https://github.com/mailhog/MailHog/blob/v1.0.0/docs/CONFIG.md

表示されているログを見ると、デフォルトでストレージはメモリ、SMTPは1025、Web UIは8025ポートのようです。

というわけで、まずは「http://[MailHogが動作しているサーバー]:8025」にアクセスすると、Web UIを参照できます。

f:id:Kazuhira:20181204234438p:plain

確認用に、smtp-cliをインストール。

$ sudo apt install  libio-socket-ssl-perl  libdigest-hmac-perl  libterm-readkey-perl libmime-lite-perl libfile-libmagic-perl libio-socket-inet6-perl
$ wget https://raw.githubusercontent.com/mludvig/smtp-cli/v3.9/smtp-cli
$ chmod a+x smtp-cli

smtp-cli — command line SMTP client

メール送信。「--server」送信先SMTPサーバーを指定。MailHogは、172.17.0.2で動作しているものとします。

$ ./smtp-cli --server 172.17.0.2:1025 --from from.user@example.com --to to.user@example.com --body-plain 'Hello Mail!'

すると、MailHog側のWeb UIでも確認できます。

f:id:Kazuhira:20181205003135p:plain

f:id:Kazuhira:20181205003143p:plain

簡単に使えそうなので、覚えておきましょう。