Halloween Sale 15% off Ends in See the plans
PHP

Catch All Email Locally With Mailpit or MailHog

On local and development sites, sends every WordPress email to a local mail catcher such as Mailpit, MailHog or the one built into Herd, Local or DDEV, so nothing reaches real inboxes.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_catcher_host = '127.0.0.1';
$wpide_catcher_port = 1025;

add_action( 'phpmailer_init', function ( $phpmailer ) use ( $wpide_catcher_host, $wpide_catcher_port ) {
    if ( ! in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
        return;
    }

    $phpmailer->isSMTP();
    $phpmailer->Host        = $wpide_catcher_host;
    $phpmailer->Port        = $wpide_catcher_port;
    $phpmailer->SMTPAuth    = false;
    $phpmailer->SMTPSecure  = '';
    $phpmailer->SMTPAutoTLS = false;
}, 999 );

Notes

Only active when WP_ENVIRONMENT_TYPE is local or development; change the host and port to match your tool (Mailpit and MailHog use port 1025).

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.