How to Test mail in localhost using Mailhog

Have you ever stuck while testing the PHP mail function in your localhost? If yes, this post is for you. Generally, mails are not working in our local system because the email server is not configured. When you upload a code to your actual server then it is working there. Today we will see how we can test mail in a localhost using Mailhog.

Mailhog is a web and API-based SMTP testing tool. It is very simple to install. Using Mailhog you can test your local mails very easily. It is similar to the very popular email testing tool MailCatcher.

Let’s see how we can install Mailhog in any Linux-based OS. While writing this article I’m using Ubuntu OS.

Install Mailhog

#install go-lang
sudo apt-get -y install golang-go
#get Mailhog
go get github.com/mailhog/MailHog
#get mhsendmail
go get github.com/mailhog/mhsendmail
#copy it to bin directory to run from anywhere in system 
sudo cp /home/{user}/go/bin/MailHog /usr/local/bin/mailhog
sudo cp /home/{user}/go/bin/mhsendmail /usr/local/bin/mhsendmail

Configure php.ini to work with MailHog

sudo subl /etc/php/7.3/apache2/php.ini
#add this line at the end of the php.ini file
sendmail_path = /usr/local/bin/mhsendmail
#restart apache
sudo service apache2 restart

Test MailHog

#start by running mailhog
mailhog

If everything is configured correctly it will give an HTTP link like this http://0.0.0.0:8025/. If you open this link, you will find a mailbox-like interface. Any mail fire from localhost can be checked here. To test local mail run the below command in the terminal. You can check by running the PHP script if the below command not working.

mhsendmail test@mailhog.local <<EOF
From: Codextblog <codextblog@mailhog.local>
To: Test <test@mailhog.local>
Subject: Test mail

Hello
Content goes here!

Thanks

EOF

More information regarding Mailhog configuration and installation can be found here.

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close

Increase Your
Magento 2
Knowledge

Get Weekly Tutorial
to your Inbox
Subscribe Me
close-link
Subscribe Here