LoginSignup
10

More than 3 years have passed since last update.

開発環境でのメール確認用にMailHogを利用する(Docker利用)

Last updated at Posted at 2020-10-13

開発環境でメールの確認方法が面倒だったり、配信先に気を使う場合がありますが、MailHogというツールを使うとローカル内に閉じたメールの確認ができる環境を簡単に立ち上げることができます。

MailHogはGo言語でできているらしいです。

以前はMailCatcherという別の類似ツールを利用していましたが、MailHogの方が構築が容易だったので乗り換えました。

docker-composeで以下の構成で立ち上げます。

ファイル構成
.
├── docker-compose.yml
└── php
    ├── docker
    │   ├── Dockerfile
    │   └── php.ini
    └── mail_test.php
docker-compose.yml

8025番ポートでwebインターフェースを利用するのでポートを設定します

version: '3'
services:
 php:
  build:
   context: ./
   dockerfile: php/docker/Dockerfile
  volumes:
   - ./php/:/var/www/html/
 mailhog:
  image: mailhog/mailhog
  ports:
   - "8025:8025" #webインターフェース用ポート
Dockerfile

phpからのメール送信にmhsendmailが必要なので、Dockerfileでインストールします。


FROM php:5.6-apache
WORKDIR /var/www/html
RUN curl -sSL https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 -o mhsendmail \
    && chmod +x mhsendmail \
    && mv mhsendmail /usr/local/bin/mhsendmail
COPY ./php/docker/php.ini /usr/local/etc/php/
php.ini

[mail function]を書き換えておきます。

[mail function]
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
; sendmail_path = /usr/sbin/sendmail -t -i
; 以下に書き換え
sendmail_path = "/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025"
mail_test.php
<?php

$to      = "hoge@localhost.local";
$subject = "TEST";
$message = "メールテスト";
$headers = "From: from@example.com";

mb_send_mail($to, $subject, $message, $headers);
docker-compose で up
$ docker-compose up -d
Building php
Step 1/4 : FROM php:5.6-apache
 ---> 24c791995c1e
Step 2/4 : WORKDIR /var/www/html
 ---> Using cache
 ---> 1294d05c5c03
Step 3/4 : RUN curl -sSL https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 -o mhsendmail     && chmod +x mhsendmail     && mv mhsendmail /usr/local/bin/mhsendmail
 ---> Using cache
 ---> db0719944c4e
Step 4/4 : COPY ./php/docker/php.ini /usr/local/etc/php/
 ---> 264eb166413a
Successfully built 264eb166413a
Successfully tagged mailhog_php:latest
WARNING: Image for service php was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling mailhog (mailhog/mailhog:)...
latest: Pulling from mailhog/mailhog
df20fa9351a1: Already exists
ed8968b2872e: Pull complete
a92cc7c5fd73: Pull complete
f17c8f1adafb: Pull complete
03954754c53a: Pull complete
60493946972a: Pull complete
368ee3bc1dbb: Pull complete
Digest: sha256:8d76a3d4ffa32a3661311944007a415332c4bb855657f4f6c57996405c009bea
Status: Downloaded newer image for mailhog/mailhog:latest
Creating mailhog_php_1     ... done
Creating mailhog_mailhog_1 ... done
mailhogのWEBインターフェース

localhost:8025で表示

image.png

phpのコンテナ内からメール送信スクリプトを実行
$ docker exec -it mailhog_php_1 /bin/bash
root@9c4b6a5df613:/var/www/html# php mail_test.php

windows上にメールの到着を通知が表示され

image.png
メールが到着していることが確認できます

image.png
image.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10