Mailhog for email test environment

Mailhog Featured Image

Mailhog Introduction

If you are developing an application which sends out emails you might need a light weight email test server. Off course you can set up a full fledged email server but that will need some hardware, network configurations, mail server setup and DNS changes. With Mailhog you can setup a light weight fake local email server to emulate SMTP service and can be up and running with email testing in minutes

mailhog dashboard

Mailhog Dashboard

What is Mailhog?

Mailhog in an open source email testing tool written in Go programming language which fakes a SMTP server. Mailhog is available for setup on multiple operating systems including Linux, Mac, FreeBSD and Windows.

Mailhog hogs emails sent to it and you can see the emails sent to it on its web dashboard.

Its code is available on Git

What is Mailhog used for?

Mailhog is used to build a quick email test environment during application development. With Mailhog you can send an email from any email address to any other email address within Mailhog.

What are some of Mailhog features?

  • Lightweight and portable
  • Extended SMTP Support
  • Multipart MIME support
  • In-memory message storage
  • Emails Capture and Storage
  • Download individual MIME parts
  • Real-time updates using EventSource

Setup Mailhog on Ubuntu 18.04

We are going to setup Mailhog on Ubuntu 18.04 but you can install it on any other operating system mentioned earlier.

We will demonstrate Mailhog setup using Golang package

Download and setup Mailhog on Ubuntu 18.04

Step 1: Hostname and DNS (optional)

Setup proper hostname of your machine because if you plan to release emails to external email servers later a FQDN is required.

Additionally if you want to access Mailhog dashboard on a URL set the DNS to point back to the public IP of the machine you are using to setup Mailhog. In our case we have setup A record pointing to the public IP of the Ubuntu VPS we are using for this blog.

Step 2: Prerequisites

Install git

sudo apt-get install git

Install golang package

sudo apt-get -y install golang-go

configure path environment variables by adding below entries to your $HOME/.profile

export GOPATH=/root/gocode

export PATH=$PATH:/usr/local/go/bin

Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, source your profile again.

# source ~/.profile

source ~/.profile

Step 3: Install Mailhog

Now you can install Mailhog using command

go get

sudo cp /root/gocode/bin/MailHog /usr/local/bin/mailhog

Step 4: Setup Mailhog to start automatically on system reboot

vi /etc/systemd/system/mailhog.service

Paste the below content

ExecStart=/usr/bin/env /usr/local/bin/mailhog > /dev/null 2>&1 &

Start mailhog automatically on system restart

systemctl daemon-reload

systemctl enable mailhog

Start and check status for mailhog service

systemctl start mailhog

systemctl status mailhog

Start mailhog and check its status

Congratulations MailHog is successfully installed. 

Configuration options for Mailhog

There are lot of options available to configure Mailhog for example you can change default ports or you can change hostname for EHLO/HELO response.

Available options can be checked with command :

mailhog -h

mailhog configuration options

Setup Mhsendmail and configure it with Mailhog to send emails

What is Mhsendmail

Mhsendmail is a sendmail like utility created to send emails via Mailhog. It ensures that your application sends emails using SMTP server listening at localhost:1025 which is handled by Mailhog.

How to install and configure Mhsendmail

Install mhsendmail using Golang:

go get

Now you need to make sure that whatever method you are using to invoke email sending utility that uses mhsendmail.

If you are on Ubuntu change sendmail path

sendmail_path = /usr/local/bin/mhsendmail

Let’s send email using mhsendmail via mailhog SMTP

For local testing you can send from mailhog.local

mhsendmail <<EOF
From: Chandan [email protected]
To: [email protected]
Subject: Test sending from mailhog.lo
Test sending from mailhog.local
Postbox Team

Once sent you can check the email in mailhog web dashboard

mhsendmail email received locally

Release email from mailhog to custom SMTP for further delivery

If you need a workflow where your application sends emails to Mailhog for testing, and after testing you want to release emails to external email addresses for final testing that can also be done. For that the receiver’s email should be the actual email address where email need to be sent:

mhsendmail <<EOF
From: Chandan [email protected]
To: [email protected]
Subject: Test sending from mailhog to
Test sending from mailhog to
Postbox Team

You will see the email sent is captured by Mailhog

Once you have tested the email you can release it to email address to which it was sent. You will need to click the release button on top of the email

mailhog release email

On next pop up screen it will ask for a SMTP server settings which you want to use to send the email. Fill in the details and click release message.

mailhog release command with custom server.

Once email is successfully released you will see the email on original email address to which it was sent.

mailhog email received externally
Issue (release message does nothing):

To send external emails please make sure that the hostname used by mailhog in EHLO/HELO does not point to localhost but to a fully qualified hostname. Unfortunately mailhog does not show any error on web UI if the email is not successfully released so you will need to check the logs for the issues.

How to send emails to Mailhog from other systems/servers

In case you need to send email from other servers/systems to mailhog that can also be done. First you will need to install mhsendmail on those other systems using the commands mentioned earlier. Then execute the mhsendmail and pass the mailhog system’s IP address as shown below:

mhsendmail -f=”[email protected]” –smtp-addr=”xx.xx.xx.xx:1025″ << EOF
From: Chandan [email protected]
To: [email protected]
Subject: Test email sending to mailhog from another server
Test email sending to mailhog from another server
Postbox Team (Test 4)

Or if you have configured your DNS with A record of the system you can even use that. In our case we have configured as an A record.

Now you can configure any system, application or script to send emails to Mailhog and you don’t necessarily need to use mhsendmail for that. Any application can pass the email to Mailhog on the SMTP port on which it is running which by default is 1025.

Hope this blog helps you to understand how to setup an email test environment using Mailhog. For any questions/queries please don’t hesitate to reach out to us.

I am Chandan Pawar working as Team Lead (Digital marketing & SysAdmin) with Postbox Consultancy Services. I enjoy building and maintaining Linux,Unix and email servers . I have done certifications RHCSA (RedHat) and MCP (Windows) and other content and digital marketing certifications from Hubspot academy .