Earlier this year, I swapped my local development environment over to Herd (along with a couple of other changes such as DBngin which is worth covering in another post).
There’s a lot to like about it one of which is how easy it is to begin capturing outgoing emails from whatever application you’re using.
Herd Pro provides an SMTP mail server on your local machine that catches all outgoing emails instead of sending them to the world. It displays them in Herds own email client and provides rich debugging capabilities for all types of emails.
Emails From WordPress in Laravel Herd
If you’re using WordPress and you’re looking for an extremely quick way to add this functionality to your local installation, add the following code to an mu-plugin:
<?php
/**
* Initializes the PHPMailer instance before it is used to send an email.
*
* This action hook is used to configure the PHPMailer instance with the necessary
* SMTP settings, such as the host, authentication, port, username, and password.
*
* @param PHPMailer $phpmailer The PHPMailer instance being initialized.
*/
add_action('phpmailer_init', function ($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = '127.0.0.1';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 2525;
$phpmailer->Username = 'WordPress';
$phpmailer->Password = '';
});
For example, I have a file – herd-mail.php – located in mu-plugins. Once this is added, any outgoing email from WordPress will be immediately captured and funneled to Herd’s email inbox for review.
Notes
PHPMailer is part of WordPress core so there’s no need to install a third-party library).
I’ve recently switched to Laravel Herd for local development. It uses Valet internally, which I obviously like, and comes with a lot of tools out of the box that make some of the overhead of development much easier.
And any utility that makes it easy to focus on actually writing code while reducing anything that deals with reading logs, configuring different versions of PHP, and even debugging is something I’m in favor of using.
Off the shelf, Herd makes debugging with PHPStorm easy; however, if you’re to get started with Laravel Herd, Xdebug, and Visual Studio Code then there’s a little more configuration to do.
Laravel Herd, Xdebug, and Visual Studio Code
A Word About Herd Documentation
I want to point out I think Herd’s documentation and file structure is one of the best I’ve seen. It’s easy to understand, easy to navigate, and easy to find various configuration files – say php.ini or debug.ini – regardless of the version of PHP you’re using.
That said, there’s not much in the way of how to configure Xdebug and Visual Studio code.
Herd, Xdebug, and Code
Here are the steps what worked for me to get my local environment setup. Note this was tested with PHP 7.4, 8.0, and 8.2.
For the following example, note I’m using Apple Silicon and am going to be using PHP 7.4. Very few things should be different save for the paths to the files references in the below code.
HERD
Changing PHP versions is trivial in Herd. This is a matter of selecting the version you want to install and/or marking it as active for your current site.
I mention this though, because it’s important to know which one you’ve got activated so you can properly update the relative Xdebug configuration.
XDEBUG
Unless you’ve set up Herd in some abnormal manner, you should find the extension in the following path:
From here, you want to open the relative php.ini file and add the following. Note that this is different than what the Herd docs show. I don’t know if it’s because they are specific to PHPStorm or not, but this is what worked for me with Visual Studio Code.
First, locate the php.ini file in the following path:
You should not need to touch the debug.ini related to the the PHP version in the same direction.
VISUAL STUDIO CODE
Finally, in Visual Studio Code, create a .vscode directory if it doesn’t already exist in the root of the project. Next, add launch.json to the directory.
You’ll likely need to restart Herd after this so issue the $ herd restart command in your terminal.
Conclusion
Now you should be able to set breakpoints, add watches, step into code, and all of the usual things you can do with the Xdebug but in a Herd environment.
Kinsta offers premium managed WordPress hosting for everyone, small or large. Powered by Google Platform and strengthened by Cloudflare, they take performance to the next level. All hosting plans include 24/7/365 support from their team of WordPress engineers. Get startedwith a free migration today.
If you are looking for WordPress guides and unbiased reviews, I recommend checking out WPKube. They also have an exclusive deals section.
Just Getting Started with WordPress? I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.