Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
require_once ABSPATH . WPINC . '/class-wp-phpmailer.php';
$phpmailer = new WP_PHPMailer( true );

/**
* Filters the PHPMailer object.
*
* Allows plugins to override the PHPMailer class with one of their own if required.
*
* @since 6.9.0
*
* @param object $phpmailer A PHPMailer (or compatible) class.
*/
$phpmailer = apply_filters( 'wp_phpmailer', $phpmailer );

$phpmailer::$validator = static function ( $email ) {
return (bool) is_email( $email );
};
Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,14 @@ function _unhook_font_registration() {
remove_action( 'init', '_wp_register_default_font_collections' );
}
tests_add_filter( 'init', '_unhook_font_registration', 1000 );

/**
* Ensure that PHPMailer is always a MockPHPMailer
*
* @param object $phpmailer The PHPMailer object.
* @return object The MockPHPMailer object.
*/
function _wp_phpmailer( $phpmailer ) {
return new MockPHPMailer();
}
tests_add_filter( 'wp_phpmailer', '_wp_phpmailer' );
23 changes: 23 additions & 0 deletions tests/phpunit/tests/pluggable/wpMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,27 @@ public function test_wp_mail_resets_properties() {
$phpmailer = $GLOBALS['phpmailer'];
$this->assertNotSame( 'user1', $phpmailer->AltBody );
}

/**
* If the global $phpmailer is overridden, it should be recreated as a real MockPHPMailer object in tests.
*
* @ticket 28618
*/
public function test_global_override_can_create_real_phpmailer() {
global $phpmailer;

$this->assertInstanceOf( 'MockPHPMailer', $phpmailer, 'The global $phpmailer should be a MockPHPMailer.' );

$phpmailer = null;

global $phpmailer;

$this->assertNull( $phpmailer, 'The global $phpmailer should be null.' );

wp_mail( 'test@example.com', 'Test Subject', 'Test Message' );

global $phpmailer;

$this->assertInstanceOf( 'MockPHPMailer', $phpmailer, 'The global $phpmailer should still be a MockPHPMailer.' );
}
}
Loading