Trapping emails with MailHog on Mac

While building a system you may need to send emails when a user signs up and tries to reset the password. The reasons are not limited to the two options and the list goes on. MailHog is an email testing tool for developers. It runs on an SMTP server which catches any messages sent to it and displays in a web interface instead of your mailbox.

In this blog, we’ll show you how to install and trap emails with MailHog on a Mac.

Open terminal and run the following command: 

brew update && brew install mailhog

Now, start MailHog by running mailhog in the command line. 

Start MailHog Server

Once MailHog is started you’ll see that the MailHog is being served on port 8025. Upon opening localhost:8025 or 0.0.0.0:8025 you’ll see the following page being rendered.

MailHog Inbox Page

It’s time to install mhsendmail so we can send emails and verify that we receive them in localhost:8025. mhsendmail is a sendmail replacement for MailHog. It ensures that the emails are redirected to MailHog using localhost:1025 as the SMTP server. Not to be confused here with SMTP and HTTP servers. The HTTP server is running on port `8025` and the SMTP server on `1025`.

To install mhsendmail you need to have Go installed. You can verify whether Go is already installed or not by running go version. If you see command not found: go you can install Go with the following Homebrew command: 

brew install go

Run go get github.com/mailhog/mhsendmail to install sendmail .

Once the installation completes, try running mhsendmail -h if you see `command not found: mhsendmail` on the command line make mhsendmail globally accessible by running:

ln -s ~/go/bin/mhsendmail /usr/local/bin/mhsendmail

To use from PHP make the following adjustments to the php.ini file 

sendmail_path = /usr/local/bin/mhsendmail

Execute the following command to test that we’re able to receive emails on MailHog:

mhsendmail [email protected] << EOF
From: App <[email protected]>
To: Test <[email protected]>
Subject: Test message

Some content!
EOF

Open localhost:8025 on your browser and you’ll see the email on your inbox. 

MailHog Inbox

anishg Written by:

Hi!, I'm Anish Ghimire. Software Developer based in Kathmandu, Nepal.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *