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.
From the docs:
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).
phpmailer_init
is a native WordPress hook.- It’s also really easy to set up Xdebug in Visual Studio Code to work with Herd. If you’re interested in learning how, review this article.