How to Send Test Emails on a Mac With MailHog

How to Send Test Emails on a Mac With MailHog

I have MAMP installed on my Mac and have been working on some WordPress sites locally. I’ve triggered some scripts, whether it be account registration or password reset, which generate emails but I’ve been unable to view them.
I’ve found numerous articles online which talk about how to setup an SMTP relay by using Gmail but I’m not crazy about having my Gmail username and password stored in plaintext in a config file on my Mac.
Then I stumbled across MailHog. In a nutshell, MailHog lets you view your outgoing email in a web UI.
To get MailHog setup on my Mac, I followed a few different articles and these are the steps that worked for me.
  1. First, I used a package manager for macOS called Homebrew. Fortunately, Homebrew is super easy to install.
  2. Open up Terminal if it’s not already. To install Mailhog, run the following command:
    brew install mailhog
  3. With MailHog installed, you’ll need to start it now by running this command:
    brew services start mailhog
  4. To check if MailHog is running, open http://127.0.0.1:8025 in your internet browser.
  5. Back in Terminal, we’ll need to edit a Postfix config file. Postfix is a mail server and alternative to the widely-used Sendmail program. Run this command:
    sudo nano /etc/postfix/main.cf
  6. Add this to the bottom of the config file:
    # For MailHog
    myhostname = localhost
    relayhost = [localhost]:1025
    If any of the previous keys exist in your configuration, comment them out.
  7. To load your changes, stop and start Postfix with this command:
    sudo postfix stop && sudo postfix start
  8. Finally, send a test email to see if everything is working. Run this command:
    date | mail -s "Test Email" your_email@gmail.com
Check http://127.0.0.1:8025 again to see if you see the test email. If not, check the Postfix queue by running mailq or tail /var/log/mail.log from the command line. This should help you spot any errors.
I will say I had trouble initially locating the Postfix error log on Sierra but this post shared the following command which helped:
log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info
Something to keep in mind is that most of the articles I read did things slightly differently. What worked for me may not work for you but there’s only one way to find out. Give it a shot!

No comments:

Post a Comment

Search