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' );
add_action( 'wp_ajax_health-check-mail-check', array( $this, 'run_mail_check' ) );
parent::__construct();
}
/**
* Checks if wp_mail() works.
*
* @uses sanitize_email()
* @uses wp_mail()
* @uses wp_send_json_success()
* @uses wp_die()
*
* @return void
*/
static function run_mail_check() {
check_ajax_referer( 'health-check-mail-check' );
if ( ! current_user_can( 'view_site_health_checks' ) ) {
wp_send_json_error();
}
$output = '';
$sendmail = false;
$email = sanitize_email( $_POST['email'] );
$email_message = sanitize_text_field( $_POST['email_message'] );
$wp_address = get_bloginfo( 'url' );
$wp_name = get_bloginfo( 'name' );
$date = date_i18n( get_option( 'date_format' ), current_time( 'timestamp' ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$time = date_i18n( get_option( 'time_format' ), current_time( 'timestamp' ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
// translators: %s: website url.
$email_subject = sprintf( esc_html__( 'Health Check – Test Message from %s', 'health-check' ), $wp_address );
$email_body = sprintf(
// translators: %1$s: website name. %2$s: website url. %3$s: The date the message was sent. %4$s: The time the message was sent.
__( 'Hi! This test message was sent by the Health Check plugin from %1$s (%2$s) on %3$s at %4$s. Since you’re reading this, it obviously works.', 'health-check' ),
$wp_name,
$wp_address,
$date,
$time,
$email_message
);
if ( ! empty( $email_message ) ) {
$email_body .= "\n\n" . sprintf(
// translators: %s: The custom message that may be included with the email.
__( 'Additional message from admin: %s', 'health-check' ),
$email_message
);
}
$sendmail = wp_mail( $email, $email_subject, $email_body );
if ( ! empty( $sendmail ) ) {
$output .= '
';
$output .= __( 'We have just sent an e-mail using wp_mail() and it seems to work. Please check your inbox and spam folder to see if you received it.', 'health-check' );
$output .= '
'; $output .= esc_html__( 'It seems there was a problem sending the e-mail.', 'health-check' ); $output .= '