Skip to main content

Command Palette

Search for a command to run...

Mailhog and Nodejs example

Updated
Mailhog and Nodejs example
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2022-05-09

This article explores the creation of a Node.js application that utilizes Mailhog, a tool for testing email setups, as a fake SMTP server. The process involves several steps, from setting up the development environment to sending test emails and verifying their delivery.

First, we must establish the necessary development environment. This begins with installing Node.js, a JavaScript runtime environment, and NPM (Node Package Manager), its associated package manager. The Node.js installer can be downloaded from the official website; the installation process is straightforward, involving a typical installer wizard. After installation, verifying the installation's success can be done using the command prompt. For Docker, a platform for running applications in containers, a video tutorial is recommended for those unfamiliar with its installation process.

The application's structure is organized using a project directory, which will house all related files. A crucial component is the docker-compose.yml file. This file, using Docker Compose, simplifies the process of managing multiple Docker containers within the application. Docker Compose allows for defining the services—in this case, Mailhog—and their configurations within a single file. The docker-compose.yml file specifies the Mailhog image to be used, its port mapping (for external access), and other relevant settings. Once this file is created, running the command docker-compose -f /path_to_file/docker-compose.yml up -d downloads the Mailhog image (if not already present) and starts it as a Docker container. This starts a Mailhog server, simulating an email server for testing purposes, without actually sending emails over the real network. The path_to_file represents the location of the docker-compose.yml file. Additional commands for managing the Docker container are available, such as stopping or removing the container.

Next, we create a package.json file using the npm init -y command. This file acts as a manifest for the Node.js project, holding metadata such as project dependencies (external libraries required by the application), scripts to automate common tasks, and the project's version. The contents of package.json will list necessary Node modules to handle email sending and other tasks. Then, the command npm install is used to download and install all these dependencies listed in the package.json. This prepares the project to interact with email servers.

A crucial part of the application is the configuration file, default.json (located within a config directory), which stores application-specific settings. This file might contain parameters like the SMTP server address (pointing to our Mailhog instance), authentication credentials (although in this case, these are not strictly necessary for Mailhog since it's a simulated server), and other relevant configuration data. This allows for easy customization and modification without directly altering the main application code.

The core application logic resides in the index.js file. This file initializes the application, sets up the Nodemailer module (a popular Node.js library for sending emails), defines the application's routes (endpoints for handling incoming requests), and starts the application on a specific port, in this case 3005. Nodemailer interacts with the configured SMTP server (Mailhog). This file contains the code to handle requests to send emails. When a request is received to send an email, this file constructs the email message using the Nodemailer API, and then sends it to the simulated Mailhog server.

The application is then launched using a command, causing it to listen for requests on port 3005. A tool like Postman can then be used to send a request to the /send endpoint, providing the email data (sender, recipient, subject, and body). Upon successful execution, the application responds with a 201 status code (indicating the request was successfully processed), and the email will be visible within the Mailhog web UI. Mailhog provides a web interface to inspect all incoming emails, allowing for inspection of the email's headers and content, which allows for validation and testing of the sending process without the complexities of dealing with actual email services.

In summary, this tutorial demonstrates a practical approach to testing email functionalities in a Node.js application using Mailhog as a simulated SMTP server. By leveraging Docker Compose and Nodemailer, the setup process is streamlined. The use of Mailhog ensures that emails are not sent to actual recipients, thus providing a safe environment for testing and development. This approach allows developers to thoroughly test their email integration without the risk of sending unintended emails or incurring expenses associated with using a live email service during development. The entire process, from setup to email sending and verification, is carefully orchestrated, using common development tools and practices, making it a valuable guide for developers working with email in Node.js applications.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.