Using Mailhog on a local Mac development environment with Laravel Valet

on

Creating an efficient local development environment is a must for any developer. One essential tool for testing email functionalities without spamming real mailboxes is Mailhog. Today, we’ll explore how to integrate Mailhog into a Laravel Valet setup on a Mac, ensuring that you can catch, view, and debug email messages generated by your applications locally.

What is Mailhog?

Mailhog is an open-source email testing tool for developers. It allows you to trap all outgoing emails from your application and view them through a web interface, making it incredibly useful for testing email functionalities without the risk of sending test emails to actual email addresses.

Mailhog is designed to simulate an SMTP server. It intercepts emails sent by your application and then displays them in a web UI. This means you can test email integrations, HTML email templates, and other email functionalities without needing to configure actual email servers or use real email addresses.

Let’s get started by installing Mailhog on a Mac setup. You should have Brew already installed before you install Mailhog. Type this into your console:

brew install mailhog

After you install Mailhog, ensure that Mailhog is running by running the following:

brew services start mailhog

Once Mailhog is running, you can access the web UI by navigating to http://localhost:8025. Any emails sent by your local development server will be captured and displayed here.

Configuring the PHP.ini File in Laravel Valet

For Mailhog to catch emails sent by PHP’s mail function, you must configure your php.ini file to use Mailhog’s SMTP server. Laravel Valet uses PHP’s built-in server, which reads its configuration from the php.ini file. To find the php.ini file that your Laravel Valet setup is using, open a terminal and run:

php --ini | grep 'Loaded Configuration File'

This command will output the path to the php.ini file that PHP is currently using. Open this file in your favorite text editor, and add the following lines to configure PHP to send mail using Mailhog:

[mail function]
; Use Mailhog for mail delivery
sendmail_path = "/usr/local/bin/mailhog sendmail catch-all@mailhog.local"

Make sure to restart Laravel Valet (valet restart) to apply the changes.

One caveat

In the above code, in the sendmail_path section, you may have to change the /usr/local/bin/mailhog location for the correct path of your Mailhog installation. In this case, we used Brew and so therefore you need to find the Brew path, instead of the one above. We can do that by entering the following in the console:

which mailhog

If you’re like me, you’ll get the following output:

/opt/homebrew/bin/mailhog

So, with that, you’ll have to replace the above code in your PHP.ini file with the following:

sendmail_path = "/opt/homebrew/bin/mailhog sendmail catch-all@mailhog.local"

Now, restart your PHP installation. This could look like the following, but replace the 8.3 with your version of PHP:

brew services restart php@8.3

All set! You should be able to send mail from anywhere in PHP and in any app within Laravel Valet, and see the following in your Mailhog UI.

Conclusion

Integrating Mailhog into your Laravel Valet setup on a Mac is a simple yet powerful way to enhance your local development environment. By capturing outgoing emails, you can effortlessly test and debug email functionalities without cluttering real inboxes. Mailhog’s ease of installation and configuration, combined with Laravel Valet’s flexibility, offers a robust platform for web application development and testing.

With Mailhog, you’re not just preventing potential email spam during development; you’re also equipped with a tool that offers insights into your email delivery process, helping you refine your application’s email functionalities before they reach your users.

Other alternatives to Mailhog

While Mailhog is a popular choice for capturing and testing emails in development environments, several other tools provide similar functionalities. Depending on your specific needs, you might find one of the following alternatives more suited to your project:

  • Papercut is a simple yet effective desktop application available for Windows. It acts as a lightweight SMTP server designed to catch and display emails sent by your development applications. It’s incredibly easy to set up and doesn’t require much configuration, making it an excellent choice for quick testing purposes.
  • Mailtrap is a cloud-based email testing tool that captures emails sent by your development and staging environments without spamming real customers. It was my preferred e-mail testing environment with use with Laravel for quite a while. It’s amazing and easy to setup!
  • FakeSMTP is a desktop application that simulates an SMTP server. Like Mailhog, it captures emails sent by your applications and allows you to view them directly within the application. It’s a Java-based tool, meaning it’s cross-platform and can run on Mac, Windows, and Linux. FakeSMTP is particularly useful for developers who need a simple, lightweight solution that doesn’t require extensive setup or configuration.
  • SMTP4Dev is a Windows and Linux tool designed for developers who need to test SMTP client code without sending real emails. It listens for SMTP emails sent from your applications and displays them in a user-friendly interface. SMTP4Dev is ideal for developers working on Windows or Linux who need an easy way to test email functionalities without external dependencies.
  • MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. It’s written in Ruby but can be run on any operating system. MailCatcher is great for developers looking for an open-source solution that can be integrated into projects of any size. It’s particularly useful for catching emails from multiple projects or applications simultaneously.

I hope you liked this blog post, let me know in the comments if this helped you out or if you have some other ideas to share!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments