From a0a659b477bf915b5b25b02e259f1a6b581b42b7 Mon Sep 17 00:00:00 2001 From: "Marius L. Jensen" <468735+Clorith@users.noreply.github.com> Date: Sat, 10 Sep 2022 20:35:10 +0200 Subject: [PATCH] Mail checker: Add the error from `wp_mail` Fixes #364 --- .../Tools/class-health-check-mail-check.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/HealthCheck/Tools/class-health-check-mail-check.php b/HealthCheck/Tools/class-health-check-mail-check.php index d000b0c9..498a2414 100644 --- a/HealthCheck/Tools/class-health-check-mail-check.php +++ b/HealthCheck/Tools/class-health-check-mail-check.php @@ -16,6 +16,8 @@ */ class Health_Check_Mail_Check extends Health_Check_Tool { + private $mail_error = null; + public function __construct() { $this->label = __( 'Mail Check', 'health-check' ); $this->description = __( 'The Mail Check will invoke the wp_mail() function and check if it succeeds. We will use the E-mail address you have set up, but you can change it below if you like.', 'health-check' ); @@ -35,13 +37,15 @@ public function __construct() { * * @return void */ - static function run_mail_check() { + public function run_mail_check() { check_ajax_referer( 'health-check-mail-check' ); if ( ! current_user_can( 'view_site_health_checks' ) ) { wp_send_json_error(); } + add_action( 'wp_mail_failed', array( $this, 'mail_failed' ) ); + $output = ''; $sendmail = false; $email = sanitize_email( $_POST['email'] ); @@ -81,6 +85,7 @@ static function run_mail_check() { } else { $output .= '

'; $output .= esc_html__( 'It seems there was a problem sending the e-mail.', 'health-check' ); + $output .= '

' . $this->mail_error->get_error_message(); $output .= '

'; } @@ -94,6 +99,17 @@ static function run_mail_check() { } + /** + * Capture errors when sending emails from WordPress. + * + * @param \WP_Error $error A WP_Error object containing the PHPMailer error. + * + * @return void + */ + public function mail_failed( $error ) { + $this->mail_error = $error; + } + /** * Add the Mail Checker to the tools tab. *