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 context this test email was sent in.
*
* @since 4.10.0
*
* @return string
*/
public function get_context() {
return $this->context;
}
/**
* Whether this test email was sent as HTML.
*
* @since 4.10.0
*
* @return bool
*/
public function is_html() {
return (bool) $this->is_html;
}
/**
* 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();
$subject = sprintf(
/* translators: %1$s - plugin name (WP Mail SMTP), %2$s - email address a test email will be sent to. */
__( '%1$s: Test email to %2$s', 'wp-mail-smtp' ),
'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' ] );
$subject = sprintf(
/* translators: %1$s - plugin name (WP Mail SMTP), %2$s - email address a test email will be sent to. */
__( '%1$s: HTML test email to %2$s', 'wp-mail-smtp' ),
'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->connection );
$this->result = $this->domain_checker->no_issues() ? self::SUCCESS : self::FAILED_DOMAIN_CHECK;
} else {
$this->result = self::SUCCESS;
}
} else {
$this->result = self::FAILED;
}
/**
* Fires after a test email has been sent and its result resolved.
*
* @since 4.10.0
*
* @param TestEmail $test_email The test email instance, carrying the result.
*/
do_action( 'wp_mail_smtp_test_email_send_after', $this ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName -- Hook name drops the repeated namespace/class segment per project convention.
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();
?>
is_pro() ) : ?>
Syed Balkhi
|
is_pro() ) : ?>
' . esc_html__( '$50 off', 'wp-mail-smtp' ) . ''
),
[ 'span' => [ 'style' => [] ] ]
);
?>
|
|
|
is_pro() ) {
// WP Mail SMTP Pro paid installed.
return $intro . $signature;
}
// Free WP Mail SMTP is installed.
$features = [
__( 'Log all emails and resend failed emails from your email log', 'wp-mail-smtp' ),
__( 'Track opens and clicks to measure the engagement', 'wp-mail-smtp' ),
__( 'Get email reports with a weekly summary of your email activity', 'wp-mail-smtp' ),
__( 'Use a backup mailer if your mail service goes down', 'wp-mail-smtp' ),
__( 'Get notified of failed emails via email, Slack, or SMS', 'wp-mail-smtp' ),
__( 'Get help from our world-class support team', 'wp-mail-smtp' ),
];
return $intro .
__( 'If you find this free plugin useful, please consider giving WP Mail SMTP Pro a try!', 'wp-mail-smtp' ) . "\n\n" .
'https://wpmailsmtp.com/lite-upgrade/' . "\n\n" .
__( 'Unlock These Powerful Features with WP Mail SMTP Pro:', 'wp-mail-smtp' ) . "\n\n" .
'+ ' . implode( "\n+ ", $features ) . "\n\n" .
$signature;
}
/**
* Set the HTML content type for a test email.
*
* @since 1.4.0
*
* @return string
*/
public static function set_test_html_content_type() {
return 'text/html';
}
}