false, 'to' => '', 'error' => __( 'That is not a valid email address.', 'adminkeep' ), 'log' => '', ); } $log = ''; $error = ''; // After Smtp::configure_mailer(), so the conversation being recorded is the real one. $debug = static function ( $phpmailer ) use ( &$log ) { // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- PHPMailer's own property names. $phpmailer->SMTPDebug = 2; $phpmailer->Debugoutput = static function ( $line ) use ( &$log ) { $log .= rtrim( (string) $line ) . "\n"; }; // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase }; $failed = static function ( $wp_error ) use ( &$error ) { $error = $wp_error instanceof \WP_Error ? $wp_error->get_error_message() : ''; }; add_action( 'phpmailer_init', $debug, Smtp::MAILER_PRIORITY + 1 ); add_action( 'wp_mail_failed', $failed ); // The debug hook must never outlive this request's test, whatever wp_mail() does. try { $sent = wp_mail( $to, /* translators: %s: site name. */ sprintf( __( 'Test email from %s', 'adminkeep' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ), __( "This is a test email sent from Adminkeep's SMTP settings. If you are reading it, your site can send email.", 'adminkeep' ) ); } finally { remove_action( 'phpmailer_init', $debug, Smtp::MAILER_PRIORITY + 1 ); remove_action( 'wp_mail_failed', $failed ); // wp_mail() reuses one global PHPMailer, so the debug settings this test turned on // would still be on for anything else the request sends, writing that conversation // into a closure this screen is done with. global $phpmailer; if ( $phpmailer instanceof \PHPMailer\PHPMailer\PHPMailer ) { // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- PHPMailer's own property names. $phpmailer->SMTPDebug = 0; $phpmailer->Debugoutput = 'error_log'; // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } } $password = $config->password(); return array( 'sent' => (bool) $sent, 'to' => $to, 'error' => SmtpScrubber::scrub( $error, (string) $config->get( 'username' ), (string) $password ), 'log' => SmtpScrubber::scrub( $log, (string) $config->get( 'username' ), (string) $password ), ); } }