Use MailHog for checking emails in the development environment (using Docker)

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.

file organization
.
├── 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);
up with docker-compose
$ 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 interface

Show on localhost: 8025 image.png

Execute mail sending script from inside php container
$ 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 image.png You can see that the email has arrived image.png image.png

Recommended Posts

Use MailHog for checking emails in the development environment (using Docker)
Build a browser test environment using Capybara in the Docker development environment
Configuration script for using docker in proxy environment on ubuntu 20.04.1
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
[Docker] Use environment variables in Nginx conf
Use docker in proxy environment on ubuntu 20.04.1
Run Redmine in the local environment of Windows10 Pro-Use Docker Desktop for Windows
I tried using Docker for the first time
ChatWork4j for using the ChatWork API in Java
Improve the performance of your Docker development environment
Allow development in Eclipse environment using iPLAss SDK
Support out of support in docker environment using centos6
Build a development environment for Docker, java, vscode
Prepare Nuxt (web) + Laravel (API) development environment in the same repository using docker-compose
[Note] Execute java program in the integrated development environment Eclipse-I tried using git
[App development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Let's finally use Docker: Start, stop, delete development environment
Docker the development environment of Ruby on Rails project
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Build Docker + Laravel PHP + Vue.js development environment in 5 minutes
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
[First team development ③] Share the development environment created with Docker
SSL in the local environment of Docker / Rails / puma
Try to build a Java development environment using Docker
Make the strongest Laravel development environment (Docker) Japan time
Command to try using Docker for the time being
Debug the VSCode + Docker + PHP development environment with XDebug.
[2021] Build a Docker + Vagrant environment for using React / TypeScript
I tried using Docker because I don't want to pollute the local environment in Microsoft Teams tab development of MS Learn
Building an environment for copying the book "Test Driven Development"
Web application development environment construction in Java (for inexperienced people)
Install by specifying the version of Django in the Docker environment
Setting the baseURL in the axios module of Docker environment Nuxt
I tried to build the environment little by little using docker
Use docker-compose.yml which is different for each environment in Makefile
WSL2 + VSCode + Docker development environment
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Use an example domain for the package name in the sample code
Install lsb_release from the command line when lsb_release fails in docker environment
Install Rails in the development environment and create a new application
Build a development environment for Django + MySQL + nginx with Docker Compose
Set up a Wordpress Docker environment without using the Worpdress image
We will build a Spring Framework development environment in the on-premises environment.
Environment construction for Servlet application development
Environment construction with Docker for beginners
Use the Findbugs plugin in Eclipse
About the current development environment (Java 8)
Install laravel/Dusk in docker environment (laravel6)
Build Java development environment (for Mac)
Hit the Docker API in Rust
Jupyter's Docker environment for running TensorFlow
[WIP] Use NFS for Docker Volume
Build Unity development environment on docker
Use cuda11.0 with pytorch using Docker
Check MySQL logs in Docker environment
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
For the time being, run the war file delivered in Java with Docker
Building an environment for "test-driven development" copying sutras starting at the terminal
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Build an environment of "API development + API verification using Swagger UI" with Docker