close

Email Testing

Email testing is important for WordPress testing because WordPress coreCore Core is the set of software required to run WordPress. The Core Development Team builds WordPress. relies on transactional emails for essential functions like user registration, password resets, and admin notifications. Capturing and verifying these emails ensures that the correct messages are triggered with proper content and formatting during the testing process.

Email Capture Methods for Different Testing Environments

Depending on your testing environment, you can use:

  • Email Logger pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party. that hooksHooks In WordPress theme and development, hooks are functions that can be applied to an action or a Filter in WordPress. Actions are functions performed when a certain event occurs in WordPress. Filters allow you to modify certain functions. Arguments used to hook both filters and actions look the same. into the wp_mail function.
  • Mailpit, a lightweight email testing tool that provides a web interface to view the captured emails.

1. Using Email Logger Plugin

This method can be used on your local development environment as well as on WordPress Playground, and doesn’t have external dependencies or require a technical configuration.

Email Logger Plugin

Steps to use:

  1. Download the plugin, install it from Plugins > Add Plugin > Upload Plugin and activate it.
  2. Use the Email Log menu item to view captured emails.

You can also use this WordPress Playground instance that comes with Email Logger plugin preinstalled.

2. Using Mailpit

Mailpit is a lightweight email testing tool that runs locally and captures all outgoing emails from your development environment, providing a web interface where you can view, inspect, and test emails without actually sending them to real recipients.

Mailpit Inbox Screenshot

You can browse emails easily through Mailpit’s user interface that acts as a mail client.

Mailpit Sample Email

Steps to install and use Mailpit:

  1. Create docker-compose.override.yml with the following code in root folder of your cloned WordPress Develop Repo.
services:
  mail:
    container_name: mailpit
    image: axllent/mailpit
    restart: unless-stopped
    ports:
      - '1025:1025' # smtp server
      - '8025:8025' # web ui
    environment:
      MP_MAX_MESSAGES: 5000
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1
    networks:
      - wpdevnet

Docker Compose Override File

  1. Run the command docker compose up -d mail in your terminal.
  2. Test access to Mailpit at `http://localhost:8025/`.
  3. Add the following snippet to your active theme’s functions.php or via Code Snippets plugin.
add_action( 'phpmailer_init', function( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host = 'localhost';
    $phpmailer->Port = 1025;
} );

add_filter( 'wp_mail_from', function( $email ) {
    return 'noreply@example.com';
});

Other Tools for Mail Testing

Conclusion

Without a mail testing mechanism like the ones described here, transactional emails triggered during testing would either fail silently or be sent to real email addresses, making it difficult to verify that WordPress core functionality is working correctly.

By using the tool suitable for your testing environment, you can capture and inspect outgoing emails to ensure they’re triggered properly with the correct content and formatting.

s
search
c
compose new post
r
reply
e
edit
t
go to top
j
go to the next post or comment
k
go to the previous post or comment
o
toggle comment visibility
esc
cancel edit post or comment