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.
Opens WPIDE on
Code
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 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.