In the development environment, the method of checking emails may be troublesome or you may be careful about the delivery destination, but if you use a tool called MailHog, you can easily set up an environment where you can check closed emails locally.
Mail Hog seems to be made in Go language.
I used to use another similar tool called MailCatcher, but I switched to MailHog because it was easier to build.
Start up with the following configuration with docker-compose.
.
├── docker-compose.yml
└── php
├── docker
│ ├── Dockerfile
│ └── php.ini
└── mail_test.php
docker-compose.yml Since the web interface is used on port 8025, set the port.
version: '3'
services:
php:
build:
context: ./
dockerfile: php/docker/Dockerfile
volumes:
- ./php/:/var/www/html/
mailhog:
image: mailhog/mailhog
ports:
- "8025:8025" #Web interface port
Dockerfile
Since mhsendmail
is required to send mail from php, install it with 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
Rewrite [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
;Rewritten below
sendmail_path = "/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025"
mail_test.php
<?php
$to = "[email protected]";
$subject = "TEST";
$message = "Email test";
$headers = "From: [email protected]";
mb_send_mail($to, $subject, $message, $headers);
$ 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
Show on localhost: 8025
$ docker exec -it mailhog_php_1 /bin/bash
root@9c4b6a5df613:/var/www/html# php mail_test.php
Notification of arrival of mail is displayed on windows You can see that the email has arrived
Recommended Posts