Skip to content

Very similar to Mailtrap but means you can host internally. It also boasts a full Swagger based API from which a client library can be generated for your language of choice.

Written in Go, Mailhog acts as a local SMTP server and captures all emails without them reaching the true recipient (for testing purposes).

This means you can then use the API to perform test assertions around email sending in your application.

Set up

Command Line Installation

Install Go

First we need to install Go. Here is an example guide.

Go get MailHog

Use go get github.com/mailhog/MailHog to install MailHog.

Confirm installation by running the help MailHog -h

Go get mhsendmail

mhsendmail is a sendmail replacement to forward mail to an smtp server.

go get github.com/mailhog/mhsendmail

To send a message from the commandline you can use:

mhsendmail test@mailhog.local <<EOF
From: App <app@mailhog.local>
To: Test <test@mailhog.local>
Subject: Test message

Some content!
EOF

Run MailHog

Use MailHog to run MailHog.

If using default config the SMTP server will be on 1025 and the dashboard will be on 8025.

Set php.ini to use mhsendmail

Set the following line in your php.ini file.

sendmail_path = /usr/local/bin/mhsendmail

PHP should now be using MailHog! To test this on a testing environment you can use the following commands:

MailHog Run - Keep in a separate tab/window

MailHog

Interactive Php shell

php -a
mail("test@example.com","My subject","test123");

Docker

You can use Docker to get an instance up and running very quickly with a one liner.

docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog

Docker Compose

version: "3"
services:
  mailhog:
    image: mailhog/mailhog:latest
    ports:
      - 1025:1025
      - 8025:8025

Usage

For emails to be sent via Mailhog, you need to configure your application (or PHP) to use it as an SMTP server. By default, Mailhog listens on port 1025 for SMTP and port 8025 for the frontend dashboard and API.

When used with Docker/Compose, you can access both the SMTP and HTTP server by localhost with the specified port. E.g.

localhost:1025 #SMTP Server
localhost:8025 #HTTP Frontend

Mailhog Dashboard

Links

Mailhog Github Page