Halloween Sale 15% off Ends in See the plans
PHP

Email Delivery Check in Site Health

Adds a test to Tools > Site Health that warns when WordPress sends mail with plain PHP mail() instead of SMTP or an API, and when an email has failed recently.

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

Code

add_action( 'wp_mail_failed', function ( $error ) {
    update_option( 'wpide_mail_last_failure', array( 'time' => time(), 'message' => $error->get_error_message() ), false );
} );

add_filter( 'site_status_tests', function ( $tests ) {
    $tests['direct']['wpide_mail'] = array(
        'label' => 'Email delivery',
        'test'  => function () {
            require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
            $mailer = new PHPMailer\PHPMailer\PHPMailer( true );
            do_action_ref_array( 'phpmailer_init', array( &$mailer ) );

            $api     = has_filter( 'pre_wp_mail' ) > 0;
            $failure = get_option( 'wpide_mail_last_failure' );
            $recent  = is_array( $failure ) && $failure['time'] > time() - WEEK_IN_SECONDS;

            $result = array(
                'label'       => 'Email is sent through SMTP or an email service',
                'status'      => 'good',
                'badge'       => array( 'label' => 'Email', 'color' => 'blue' ),
                'description' => '<p>WordPress emails go through a dedicated mail server, which is the most reliable setup.</p>',
                'actions'     => '',
                'test'        => 'wpide_mail',
            );

            if ( 'smtp' !== $mailer->Mailer && ! $api ) {
                $result['status']      = 'recommended';
                $result['label']       = 'Email is sent with PHP mail()';
                $result['description'] = '<p>Emails sent with PHP mail() often land in spam or are blocked. Configure SMTP or an email API for reliable delivery.</p>';
            }

            if ( $recent ) {
                $result['status']       = 'critical';
                $result['label']        = 'An email failed to send recently';
                $result['description'] .= '<p>Last failure: ' . esc_html( wp_date( 'Y-m-d H:i', $failure['time'] ) . ' – ' . $failure['message'] ) . '</p>';
            }

            return $result;
        },
    );

    return $tests;
} );

Notes

Failures are recorded from the moment the snippet is active; the warning clears seven days after the last failure.

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.