Hazem Allbabidi

September 27, 2024 | 4 min read


Open Source: MailHog for Testing Emails

In this series titled “Open Source” we go through an open-source tool that solves specific problems. In this article, we go through a tool called MailHog, an open-source tool that allows you to test sending emails locally.

What is MailHog?

MailHog is a tool built using Go to allow users to create SMTP servers locally that allows you to receive and view messages through a simple dashboard. Once installed and running, it provides you with a URL in which it can receive emails on.

Installation

To install MailHog on Ubuntu or Debian, we first need to make sure we have Go installed. We can install it by running the following command:

sudo apt-get -y install golang-go

To make sure it was installed properly, run the following command, which will print out the Go version installed:

go version

Now to install MailHog, we can run the following command:

go get github.com/mailhog/MailHog

Now MailHog should be installed in the following directory:

~/go/bin/

You can make sure it is there by going to the directory and listing the files there.

Running The Tool

To start the tool, simply type in the full path of it in the command-line:

~/go/bin/MailHog

Assuming the command works, you will see an output similar to this:

2024/09/27 23:11:27 Using in-memory storage
2024/09/27 23:11:27 [SMTP] Binding to address: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
2024/09/27 23:11:27 Serving under http://0.0.0.0:8025/
Creating API v1 with WebPath: 
Creating API v2 with WebPath:

The application will be running on two different ports, the first being 1025 which is where the actual SMTP server is running, and the second port being 8025 where you can a simple dashboard that includes the emails being received by MailHog.

Testing and Using The Tool

In order to use the tool, we will create a simple Node JS file which sends emails to the MailHog SMTP server. To do that, create an empty directory where we will be creating all the MailHog files, and then access it:

mkdir testing_mailhog
cd testing_mailhog

Now, create the package.json file by running:

npm init -y

Next, we will install a package titled nodemailer which will allow us to send emails:

npm install nodemailer

We can move on to creating the main file which will allow us to send the emails:

touch index.js

Now, using your favorite IDE or code editor, insert the following into the index.js file:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
	service: 'smtp',
	host: '0.0.0.0',
	port: '1025'
})

What we are doing here, is importing the nodemailer package and then creating a transporter which handle sending the emails. The transporter will take in information about the method we will use to send the emails. The service we are using is smtp, the host will be whatever MailHog provided you (which is usually 0.0.0.0), and finally, we add the port of the SMTP server, which is 1025.

Next, append the following code to the index.js file:

transporter.sendMail({
	from: 'sender@mailhog.com',
	to: 'receiver@mailhog.com',
	subject: 'Testing Mailhog using Node JS',
	text: 'Successfully sent a message to Mailhog using Node JS!'
}, function(error, info) {
	if(error) {
		console.log(error)
	} else {
		console.log("Email sent successfully!")
	}
})

This code will use the transporter we created to send an email. This takes in 4 main pieces of information:

  1. from - this is the email address of the sender, which is dependant on the email service you use. Since we are using MailHog, we can use any email address
  2. to - this is the email address of the email receiver. Since it is also going to the MailHog SMTP server, it can be any email address
  3. subject - this is the subject of the email
  4. text - this is the body or content of the email. It takes in regular text. We can also replace it with the key html to send HTML instead of normal text

Finally, save your file, make sure MailHog is up and running, and run the command:

node index.js

If you have gotten the output Email sent successfully!, go to your browser and type in the URL:

http://localhost:8025

This will open the MailHog webpage which displays all the emails you have received.

Assuming the email was sent successfully, you should it there in the middle area of the web page.

Conclusion

MailHog is a great tool that I have been using for years and has helped me in making sure that the email service I have is working fine and in seeing how emails look like without having an actual SMTP server.

I hope you enjoyed this article and benefitted from it. See you in the next one!


Previous

AWS in a Glance
Sign Up To Binance To Get 10% Off Commission Fees Sign Up To Kucoin