Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active March 10, 2022 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/073bf998b8265a5a360decba88948ca5 to your computer and use it in GitHub Desktop.
Save ipokkel/073bf998b8265a5a360decba88948ca5 to your computer and use it in GitHub Desktop.
Debugging email for WordPress. Logs mail erorrs,
<?php
/**
* WordPress Mail Debugger
*
* Log mail error message to debug-mail.log in wp-contents folder.
* Log mail error to debug.log in wp-contents folder.
*/
function wp_mail_error_log( $wp_error ) {
// Log mail error message to debug-mail.log file in wp-contents folder
$fn = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'debug-mail.log';
$fp = fopen( $fn, 'a' );
fputs( $fp, date( '[Y-m-d H:i:s] ' ) . 'Mailer error: ' . $wp_error->get_error_message() . "\n" );
fclose( $fp );
// Log mail error in the debug.log in wp-contents folder
error_log( '##--WP MAIL ERRROR --##' );
error_log( print_r( $wp_error, true ) );
}
add_action( 'wp_mail_failed', 'wp_mail_error_log', 10, 1 );
<?php
/**
* Call wp_mail function with invalid arguments.
*
* Can be used for testing
*/
function force_mail_fail() {
wp_mail( '', 'Subject', '' ); // returns false, the email was not sent
}
add_action( 'init', 'force_mail_fail' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment