How to Use MailHog to Test Emails Locally | Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello, I'm Maria. And today, I'm going to show you how to test emails using MailHog. (upbeat music) Testing emails in a local development environment can be challenging. It's a real pain to know whether your website's or web app's outgoing emails are actually reaching the recipient's inbox. Enter MailHog. MailHog is an easy email testing tool that makes it super easy to install and configure a local email server. In this video, you'll learn the ins and outs of MailHog, including its installation, configuration, how to test emails locally, and much more. But before we get too far, I want to let you know that there will be links to more resources in the video's description. And remember, subscribe and ring that bell to get notifications for future helpful content. Ready to dive in? Let's go. MailHog is an open source email testing tool primarily aimed at developers. It enables you to test your web app's email sending and receiving capabilities more efficiently. Built with the Go programming language, MailHog can be run on multiple operating systems, including Windows, Linux, FreeBSD and macOS. But why do you even need MailHog in the first place? MailHog solves many of email testings major problems. Say you're developing a WordPress website in a local development environment. If you want to test a contact form or any outgoing email from it, it can be a dire task. Typically, the web application's default SMTP server takes care of this task. In a local development environment, it almost always never works due to multiple reasons. First, you need to set up your operating system, web server and web app to enable emails in a local environment. Then you'll have to make sure that the email's arrived successfully in your recipient's inbox, which can end up wasting your time and inbox space. And then there's an issue of using a real email address for testing. It can hurt your private emails' credibility. MailHog solves all these issues. It sets up a fake SMTP server that you can set your web application to send and receive emails. It even stores the sent and received emails in a nifty web UI. So you can go through them just like you would with a real email inbox. Lastly, MailHog also lets you retrieve those emails with an API for automated testing. It's a powerful feature, and how you use the API is up to you. MailHog comes equipped with many features out of the box. Here are some of the major highlights. Lightweight and portable. You don't need to install MailHog to use it. It's one of MailHog's major advantages. That makes it highly portable to run it on almost any operating system or web server. Extended SMTP support. Not only does MailHog support setting up a fake SMTP server, but you can also use it to set up an ESMTP server. It also includes support for SMTP authorization and pipelining. API support. You can use MailHog's built-in HTTP API to retrieve, list, or delete emails. Failure testing. You can invite Jim, MailHog's chaos monkey... Yes, it's actually called that. To the party to test out your web app's email deliverability. It does this by randomly creating common email deliverability problems, such as rejected connections, authentications and rate limited connections. Real-time updates. MailHog uses the event source web interface to provide instantaneous updates. Emails capture and storage. The captured emails can be stored in in-memory message storage and persisted in a MongoDB database and file-based storage system. Download attachments. MailHog supports the download of individual MIME parts. Web UI. You can use MailHog's web UI to view emails as plain text, HTML, or source. It also supports multi-part MIME and encoded headers. Authentication. MailHog supports HTTP basic authentication for both its web UI and API. Release captured emails. You can configure MailHog to release captured emails to real SMTP servers for delivery. Apart from these features, MailHog currently has two client libraries for PHP and Node.js environments to interact with the MailHog API easily. As mentioned before, MailHog doesn't need to be installed. However, you still have to download it and set it up in your development environment. Let's get started with downloading MailHog. Then we'll show you how to set it up on various platforms. There are many ways you can go about doing it. First, head to MailHog releases page on GitHub to download its latest stable version for Windows. After downloading MailHog, you can run it as is without any external dependencies. As of filming this video, MailHog 1.0.1 is the latest release version. You can download it pre-compiled for Windows, Linux, and Darwin platforms. If you're using a niche platform, you can also download its source code and compile it for your specific use case. As MailHog for Windows is a simple, self-executable program, you can just run the downloaded exe file. If you're presented with any security alerts, make sure to allow MailHog to run on your local host environment without firewall restrictions. Here's how MailHog looks on Windows 10. You can see that the SMTP server is on port 1025 and the HTTP server is on port 8025. Nothing too fancy yet, but it's a confirmation that MailHog is running on your system. Now you can plug these addresses into your browser to view the MailHog web UI. You can not find any email listed here yet as we haven't configured any of our web applications to use MailHog. We'll cover that later in this video. If you're using Linux, here's how you can deploy MailHog. The first command installs the golang programming language on your development environment. You can skip this line if you've installed Go already. The next command installs git on your machine. The third command uses Go's directive to get MailHog's latest stable release from its GitHub repository. Next, you can run path to MailHog through your terminal to start MailHog. The exact path may vary depending on your Linux flavor. As Go's bin files on Ubuntu are stored under the Go bin directory, you can run this code to start MailHog. That's pretty much it. MailHog is not running on your Linux machine. This is how it looks. If you want to start MailHog automatically on every login, you can set it to start as a demon server using Supervisor or Upstart, et cetera. Just like with Windows, you can see that the SMTP server starts on port 1025 and the HTTP server starts on port 8025. You can visit these addresses in your web browser to see the MailHog web UI. Apple users can install MailHog using Homebrew, open up your terminal and run the following command. That'll install MailHog on your macOS. Next, you can start MailHog with this command. If you want MailHog to launch automatically on login and run as a background service, you can run the following command instead. That's pretty much it. MailHog is now installed on your macOS. You can view its web UI by visiting these addresses in your browser. Docker is one of the easiest ways to set up MailHog across any platform. First, make sure that you have Docker installed on your machine. Next, you can start MailHog directly from Docker hub by running the following command. Let's break down the command and its directives. Docker run starts a Docker container. d directive runs the Docker container in the background. p 1025:1025 and -p 8025:8025 directives expose MailHog's default SMTP ports to your local ones. mailhog/mailhog grabs the latest MailHog build from Docker hub. Alternatively, if you want to mount MailHog's mail directory to your local file system, you can run the following command. Like before, you can visit these addresses in your browser to use MailHog web UI. Docker Compose is an extension of Docker that makes it easy to run multiple Docker containers with a single command. You do this by defining all the containers you want to run in a yaml file. By default, Docker Compose requires you to name this file as docker-compose.yaml. To run MailHog with Docker Compose, here's how your Docker Compose yaml file should look. Inside the folder where this file resides, run the following command to start MailHog in the background. It works exactly like with Docker, except here, you don't have to mention many directives as you've defined them already. You can now enter these addresses in your browser to view MailHog web UI. MailHog redirects emails from web apps to its SMTP server using mhsendmail, a Sendmail replacement created specifically for MailHog. It ensures that your web apps emails are sent and received using local host's 1025 SMTP server, which MailHog is configured to handle by default. To get started, you need to download and install mhsendmail. Depending on your platform, you can approach this in various ways. For instance, if you're using Ubuntu, you can download mhsendmail from its GitHub repository by using the following command. Next, make this file executable and move it to your user directory. To do this, use the following command. Alternatively, you can also get mhsendmail on Go link or Homebrew by running this command, but make sure that Go is installed first. For installing Go with Homebrew, run the following command. With mhsendmail downloaded, you can now proceed with installing it. Next, you need to configure your web app to use MailHog's mhsendmail as it's email routing service. Most Linux systems have it set to usendmail by default. You can do this by editing your php.ini file. On most servers, you can find the php.ini file in the root directory. You can also execute the following command to find where it is. For Linux machines running Ubuntu, you need to set the value of Sendmail path to... Instead of this value, you can also use the value shown here to get the same result. If you used Go to install mhsendmail, or if you're using any other variant of Linux, the path may vary slightly depending on your platform or configuration. Make sure to link to the proper mhsendmail directory. Mac users need to edit their postfix config file to set up MailHog properly. You can use the following postfix configuration. For WordPress sites, you can use the free MailHog for WordPress plugin to route your site's emails to MailHog. To use it, install and activate the plugin in your WordPress admin dashboard. A better alternative is to use DevKinsta. We'll cover it in depth later. You can configure MailHog using its various environment variables or command line options. For example, you can change its default host name from mailhog.example to a custom name by using the MH_HOSTNAME environment variable, or the -hostname command line option. Finally, let's test our MailHog setup to see whether it's configured properly. You can send an email with MailHog's CLI by using the following mhsendmail directives. In this code, the EOF directive sets accustomed delimiter so that you can enter the test email details interactively inside the terminal. It executes all the input when you type in the delimiter again. Here, it sends a mail using the mhsendmail command. You can verify whether the email was delivered successfully by visiting the MailHog web UI. You can also click on the test email to verify its body content. That's a wrap. We can now conclude that MailHog has been configured successfully. Adding to this method, there are other ways you can send a test email. For instance, on macOS, you can send a test email by running the following command in your terminal. On Windows, you can send an email with PowerShell by executing the following code. You can also define a PHP script with all the mail data and use PHP's mail function to send a test email. To do that, define your PHP script like this and execute it from the terminal. Our local WordPress development tool, DevKinsta, uses MailHog to power its email testing features, as it also uses Docker to containerize individual sites. You'll have a separate MailHog inbox for every local site you're developing. Like MailHog web UI, you can use DevKinsta's local email inbox to view and manipulate emails. You can also check for DevKinsta's MailHog logs by running the following Docker command. Like MailHog, DevKinsta is a great way to get started with local WordPress development. Not only does it include MailHog for testing emails locally, but it also comes preloaded with Adminer for easy database management. With MailHog and DevKinsta on your side, you'll find that testing emails locally has never been easier. Kinsta's WordPress hosting can speed up your website by up to 200%, and you'll get 24/7 support from our expert WordPress engineers. Let us show you the Kinsta difference. Try a free demo of our MyKinsta Dashboard at demo.kinsta.com. Thank you for watching. Don't forget to subscribe for more tutorials, explainers, and helpful content like this. (upbeat music)
Info
Channel: Kinsta
Views: 20,750
Rating: undefined out of 5
Keywords: mailhog, what is mailhog, mailhog tutorial, mailhog smtp server, mailhog port, mailhog linux, mailhog mac, Local Email Testing, devkinsta, mailhog docker, how to use mailhog, mailhog for windows, mailhog localhost
Id: yozMCBg2sBk
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Mon Oct 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.