Halloween Sale 15% off Ends in See the plans
PHP

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.

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

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

  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.