connection = $connection; return $this; } /** * Set the caller context (drives the X-Mailer-Type header). * * @since 4.9.0 * * @param string $context One of the CONTEXT_* constants. * * @return self */ public function with_context( $context ) { $this->context = (string) $context; return $this; } /** * Toggle HTML vs plain text body. * * @since 4.9.0 * * @param bool $is_html True for HTML, false for plain text. * * @return self */ public function as_html( $is_html ) { $this->is_html = (bool) $is_html; return $this; } /** * Toggle DomainChecker invocation after a successful send. * * @since 4.9.0 * * @param bool $run True to run the domain check, false to skip. * * @return self */ public function with_domain_check( $run ) { $this->run_domain_check = (bool) $run; return $this; } /** * Whether the send succeeded (with or without domain-check issues). * * @since 4.9.0 * * @return bool */ public function is_successful() { return $this->result === self::SUCCESS || $this->result === self::FAILED_DOMAIN_CHECK; } /** * Get the raw result constant. * * @since 4.9.0 * * @return int|null */ public function get_result() { return $this->result; } /** * Get the DomainChecker instance populated after a successful send (when enabled). * * @since 4.9.0 * * @return DomainChecker|null */ public function get_domain_checker() { return $this->domain_checker; } /** * Get the connection used for the send. * * @since 4.9.0 * * @return ConnectionInterface|null */ public function get_connection() { return $this->connection; } /** * Send the test email. * * Lifted from TestTab::process_post() with the form-data plumbing replaced by * fluent setters; the wp_mail() / ob_start / DomainChecker block stays identical. * * @since 4.9.0 * * @param string $recipient Recipient email address. * * @return int|null Result constant (SUCCESS, FAILED, FAILED_DOMAIN_CHECK), or null when validation failed. */ public function send( $recipient ) { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks -- Paired add_filter/remove_filter scope the HTML content-type to this single test send only; moving them to hooks() would change behavior. $recipient = filter_var( wp_unslash( $recipient ), FILTER_VALIDATE_EMAIL ); if ( empty( $recipient ) ) { return null; } if ( ! $this->connection ) { $this->connection = wp_mail_smtp()->get_connections_manager()->get_primary_connection(); } $phpmailer = wp_mail_smtp()->get_processor()->get_phpmailer(); /* translators: %s - email address a test email will be sent to. */ $subject = 'WP Mail SMTP: ' . sprintf( esc_html__( 'Test email to %s', 'wp-mail-smtp' ), $recipient ); $headers = [ 'X-Mailer-Type:' . $this->context ]; if ( $this->is_html ) { add_filter( 'wp_mail_content_type', [ __CLASS__, 'set_test_html_content_type' ] ); /* translators: %s - email address a test email will be sent to. */ $subject = 'WP Mail SMTP: HTML ' . sprintf( esc_html__( 'Test email to %s', 'wp-mail-smtp' ), $recipient ); $headers[] = 'Content-Type: text/html'; } // Send the test mail. $result = wp_mail( $recipient, $subject, $this->get_email_message( $this->is_html ), $headers ); if ( $this->is_html ) { remove_filter( 'wp_mail_content_type', [ __CLASS__, 'set_test_html_content_type' ] ); } /* * Notify a user about the results. */ if ( $result ) { if ( $this->run_domain_check ) { $connection_options = $this->connection->get_options(); $mailer = $connection_options->get( 'mail', 'mailer' ); $email = $connection_options->get( 'mail', 'from_email' ); $domain = ''; // Add the optional sending domain parameter. if ( in_array( $mailer, [ 'mailgun', 'sendinblue', 'sendgrid' ], true ) ) { $domain = $connection_options->get( $mailer, 'domain' ); } $this->domain_checker = new DomainChecker( $mailer, $email, $domain ); $this->result = $this->domain_checker->no_issues() ? self::SUCCESS : self::FAILED_DOMAIN_CHECK; } else { $this->result = self::SUCCESS; } } else { $this->result = self::FAILED; } return $this->result; } /** * Get the email message that should be sent. * * @since 1.4.0 * * @param bool $is_html Whether to send an HTML email or plain text. * * @return string */ private function get_email_message( $is_html = true ) { // Default plain text version of the email. $message = self::get_email_message_text(); if ( $is_html ) { $message = $this->get_email_message_html(); } return $message; } /** * Get the HTML prepared message for test email. * * @since 1.4.0 * * @return string */ private function get_email_message_html() { ob_start(); ?>
|