Email Check plugin: settings in code and WP-CLI

Since version 1.3.0, the nanoPost Email Check plugin can take its settings from code and be run from the command line. This page is for whoever manages a site’s configuration, such as an agency that sets up many sites the same way. The setup page covers the plugin screen.

Settings in code

Add any of these constants to wp-config.php or a must-use plugin:

define( 'NANOPOST_EMAIL_CHECK_RECIPIENT', '[email protected]' );
define( 'NANOPOST_EMAIL_CHECK_INTERVAL', 'daily' ); // twicedaily, daily or weekly
define( 'NANOPOST_EMAIL_CHECK_WEBHOOK_URL', 'https://hooks.slack.com/services/...' );

The recipient takes the same values as the plugin screen: one address or, in version 1.6.0 and later, up to 3 separated by commas, for example '[email protected], [email protected]'.

You can set one, two or all three constants. A value set in code wins over the plugin screen. The screen shows that field read-only, with a note such as Set in code (NANOPOST_EMAIL_CHECK_RECIPIENT).

A value from code must pass the same checks as the plugin screen. An invalid value is ignored, the previous valid value stays in use, and an error notice at the top of the plugin screen names the value that was ignored.

On a multisite network, a constant in wp-config.php applies to every site.

The filter

When a value depends on the site, use the nanopost_email_check_settings filter. It receives the settings as an array with the keys recipient, interval and webhook_url. An empty recipient means the site’s Administration Email Address; an empty webhook_url means no alerts. The filter runs after the constants, so it can change a value a constant set. For example, to check the main site of a network twice a day:

add_filter( 'nanopost_email_check_settings', function ( $settings ) {
    if ( is_main_site() ) {
        $settings['interval'] = 'twicedaily';
    }
    return $settings;
} );

A value the filter changes is shown read-only with the note Set in code (nanopost_email_check_settings filter). It is checked the same way as a constant.

Use WP-CLI

The wp email-check command runs a check, shows the status, sends a test alert and reads or changes the saved settings. On a multisite network, choose the site with --url. For the full help, run wp help email-check.

Command What it does Exit codes
wp email-check run Sends a test email now. The log shows WP-CLI under Started by. 0 passed, 1 failed
wp email-check status Shows whether the checks run and pass. Add --format=json for scripts. 0 ok, 1 last check failed, 2 checks stopped or not scheduled, 3 no check has run yet (version 1.3.1 and later)
wp email-check test-alert Sends a test alert to the saved webhook URL. 0 receiver accepted it, 1 otherwise
wp email-check settings get Shows the settings in use, saved or set in code. Add a key to show one value.
wp email-check settings set <key> <value> Changes a saved setting: recipient, interval or webhook_url. 1 when the value is invalid or set in code; nothing changes

settings set checks a value with the same rules as the plugin screen. It cannot change a setting that is set in code. For more than one recipient (version 1.6.0 and later), quote the list: wp email-check settings set recipient "[email protected], [email protected]". run and test-alert have no once-a-minute limit.

As on the plugin screen, a passing check means wp_mail() reported success. It does not confirm that the message arrived; what a pass means explains the difference.

In a monitoring script

The exit code of status lets a server cron job or a monitoring tool raise its own alert. For example:

if ! wp email-check status > /dev/null; then
  echo "Email Check needs attention on $(wp option get home)"
fi

A nonzero code needs a look: 1 means the last check failed; 2 means checks have stopped or are not scheduled; 3 means the checks are scheduled but none has run yet. Version 1.3.0 exits with 0 in that last case.

Remove the plugin with WP-CLI

Use wp plugin uninstall nanopost-email-check --deactivate. It removes the plugin’s settings, log and scheduled event. wp plugin delete removes only the files and leaves that data in the database.