Admin Notice When an Email Fails to Send
Shows administrators a warning in the dashboard when WordPress could not send an email, with the error message, so delivery problems are noticed quickly.
Opens WPIDE on
Code
add_action( 'wp_mail_failed', function ( $error ) {
set_transient( 'wpide_last_mail_error', array(
'message' => $error->get_error_message(),
'time' => time(),
), DAY_IN_SECONDS );
} );
add_action( 'admin_notices', function () {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$error = get_transient( 'wpide_last_mail_error' );
if ( ! $error ) {
return;
}
delete_transient( 'wpide_last_mail_error' );
printf(
'<div class="notice notice-error"><p><strong>An email could not be sent</strong> (%1$s): %2$s</p></div>',
esc_html( human_time_diff( (int) $error['time'] ) . ' ago' ),
esc_html( $error['message'] )
);
} );
Notes
The notice appears once per failure and can be dismissed by reloading. Pair it with an SMTP snippet to fix delivery.
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.