# Log Failed Emails

> Writes a line to the PHP error log whenever wp_mail() fails, with the subject, recipients and error message, to help debug delivery problems.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, debugging, logs
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/log-failed-emails/

## Code

```php
add_action( 'wp_mail_failed', function ( $error ) {
    $data    = (array) $error->get_error_data();
    $to      = isset( $data['to'] ) ? implode( ', ', (array) $data['to'] ) : 'unknown';
    $subject = isset( $data['subject'] ) ? (string) $data['subject'] : '';

    error_log( sprintf( '[wp_mail] Could not send "%s" to %s: %s', $subject, $to, $error->get_error_message() ) );
} );
```

## Notes

The log contains recipient addresses. Enable WP_DEBUG_LOG or check your server error log.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Log Failed Emails" 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/
