Last active
March 10, 2022 08:45
-
-
Save ipokkel/073bf998b8265a5a360decba88948ca5 to your computer and use it in GitHub Desktop.
Debugging email for WordPress. Logs mail erorrs,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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