Skip to content

Commit

Permalink
Mail checker: Add the error from wp_mail
Browse files Browse the repository at this point in the history
Fixes #364
  • Loading branch information
Clorith authored Sep 10, 2022
1 parent 73f175d commit a0a659b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion HealthCheck/Tools/class-health-check-mail-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>wp_mail()</code> 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' );
Expand All @@ -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'] );
Expand Down Expand Up @@ -81,6 +85,7 @@ static function run_mail_check() {
} else {
$output .= '<div class="notice notice-error inline"><p>';
$output .= esc_html__( 'It seems there was a problem sending the e-mail.', 'health-check' );
$output .= '</p><p>' . $this->mail_error->get_error_message();
$output .= '</p></div>';
}

Expand All @@ -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.
*
Expand Down

0 comments on commit a0a659b

Please sign in to comment.