diff --git a/tests/test-vip-mail.php b/tests/test-vip-mail.php index 3368c91c33..22159eda8d 100644 --- a/tests/test-vip-mail.php +++ b/tests/test-vip-mail.php @@ -150,6 +150,40 @@ public function test_smtp_servers_not_overwritten(): void { self::assertEquals( $expected, $mailer->Host ); } + public function test_smtp_servers_not_overwritten_when_not_present_in_host_overwrite_allow_list(): void { + Constant_Mocker::define( 'VIP_SMTP_HOST_OVERWRITE_ALLOW_LIST', 'server1,not-preset-server,server2' ); + + $GLOBALS['all_smtp_servers'] = [ 'server1', 'server2' ]; + + $expected = 'preset-server'; + + add_action( 'phpmailer_init', function ( PHPMailer &$phpmailer ) use ( $expected ) { + $phpmailer->isSMTP(); + $phpmailer->Host = $expected; + } ); + + wp_mail( 'test@example.com', 'Test', 'Test' ); + $mailer = tests_retrieve_phpmailer_instance(); + + self::assertEquals( $expected, $mailer->Host ); + } + + public function test_smtp_servers_are_overwritten_when_present_in_host_overwrite_allow_list(): void { + Constant_Mocker::define( 'VIP_SMTP_HOST_OVERWRITE_ALLOW_LIST', 'server1,overwritable-host,server2' ); + + $GLOBALS['all_smtp_servers'] = [ 'new-host' ]; + + add_action( 'phpmailer_init', function ( PHPMailer &$phpmailer ) { + $phpmailer->isSMTP(); + $phpmailer->Host = 'overwritable-host'; + } ); + + wp_mail( 'test@example.com', 'Test', 'Test' ); + $mailer = tests_retrieve_phpmailer_instance(); + + self::assertEquals( 'new-host', $mailer->Host ); + } + /** * @ticket GH-3638 */ diff --git a/vip-mail.php b/vip-mail.php index 59eb0687ea..abf4523b55 100644 --- a/vip-mail.php +++ b/vip-mail.php @@ -102,7 +102,11 @@ public function phpmailer_init( &$phpmailer ): void { return; } - if ( 'smtp' === $phpmailer->Mailer ) { + $host_overwrite_allow_list = defined( 'VIP_SMTP_HOST_OVERWRITE_ALLOW_LIST' ) + ? array_map( 'trim', explode( ',', constant( 'VIP_SMTP_HOST_OVERWRITE_ALLOW_LIST' ) ) ) + : []; + + if ( 'smtp' === $phpmailer->Mailer && ! in_array( $phpmailer->Host, $host_overwrite_allow_list, true ) ) { return; }