HEX
Server: LiteSpeed
System: Linux cde2.duelhost.dk 4.18.0-553.34.1.lve.el8.x86_64 #1 SMP Thu Jan 9 16:30:32 UTC 2025 x86_64
User: dtptviut (1121)
PHP: 8.0.30
Disabled: exec,system,passthru,shell_exec,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/dtptviut/public_html/wp-content/plugins/easy-wp-smtp/src/WPCLI/Commands/Test.php
<?php

namespace EasyWPSMTP\WPCLI\Commands;

use WP_CLI;
use EasyWPSMTP\Options;
use EasyWPSMTP\TestEmail\TestEmail;

/**
 * Send a test email via the currently configured mailer.
 *
 * @since 2.15.0
 */
class Test {

	/**
	 * Send a test email via the currently configured mailer.
	 *
	 * ## OPTIONS
	 *
	 * <recipient>
	 * : Email address to send the test message to.
	 *
	 * [--plain]
	 * : Send as plain text instead of HTML.
	 *
	 * ## EXAMPLES
	 *
	 *     wp easy-wp-smtp test you@example.com
	 *     wp easy-wp-smtp test you@example.com --plain
	 *
	 * @since 2.15.0
	 *
	 * @param array $args       Positional args.
	 * @param array $assoc_args Associative args.
	 *
	 * @return void
	 */
	public function __invoke( $args, $assoc_args ) { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks -- The wp_mail_failed capture hook is paired add/remove and scoped to this single send.

		$recipient = $args[0] ?? null;

		if ( $recipient === null || ! is_email( $recipient ) ) {
			WP_CLI::error( __( 'Pass a valid recipient: wp easy-wp-smtp test <recipient>', 'easy-wp-smtp' ) );
		}

		$mailer = Options::init()->get( 'mail', 'mailer' );

		if ( $mailer === '' || $mailer === 'mail' ) {
			WP_CLI::error( __( 'No mailer is configured. Run `wp easy-wp-smtp setup ...` first.', 'easy-wp-smtp' ) );
		}

		// Capture wp_mail_failed to surface the underlying WP_Error on failure.
		$captured_error = null;
		$capture        = static function ( $wp_error ) use ( &$captured_error ) {
			$captured_error = $wp_error;
		};
		add_action( 'wp_mail_failed', $capture );

		$test = ( new TestEmail() )->as_html( ! isset( $assoc_args['plain'] ) );

		$test->send( $recipient );

		remove_action( 'wp_mail_failed', $capture );

		if ( $test->is_successful() ) {
			WP_CLI::success(
				sprintf(
					/* translators: %1$s is the recipient email address. %2$s is the mailer slug (e.g. smtp, sendgrid). Recipient and mailer slug are not translated. */
					__( 'Test email sent to %1$s via mailer "%2$s".', 'easy-wp-smtp' ),
					$recipient,
					$mailer
				)
			);

			return;
		}

		$reason = $captured_error instanceof \WP_Error
			? $captured_error->get_error_message()
			: __( 'wp_mail() returned false (no further detail available).', 'easy-wp-smtp' );

		WP_CLI::error(
			sprintf(
				/* translators: %s is the underlying error message returned by wp_mail() / the mailer (already localized or quoted verbatim from the mailer response). */
				__( 'Test email failed: %s', 'easy-wp-smtp' ),
				$reason
			)
		);
	}
}