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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.