# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, deliverability, admin, notices
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-failure-admin-notice/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Admin Notice When an Email Fails to Send" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
