diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index ffcd2bdcc3..fdbfdea250 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -378,12 +378,16 @@ if ( ! function_exists( 'wp_mail' ) ) : */ if ( ! isset( $from_email ) ) { // Get the site domain and get rid of www. - $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); - if ( 'www.' === substr( $sitename, 0, 4 ) ) { - $sitename = substr( $sitename, 4 ); - } + $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); + $from_email = 'wordpress@'; - $from_email = 'wordpress@' . $sitename; + if ( null !== $sitename ) { + if ( 'www.' === substr( $sitename, 0, 4 ) ) { + $sitename = substr( $sitename, 4 ); + } + + $from_email .= $sitename; + } } /** diff --git a/tests/phpunit/tests/mail.php b/tests/phpunit/tests/mail.php index 66a613234b..8bf0a083c9 100644 --- a/tests/phpunit/tests/mail.php +++ b/tests/phpunit/tests/mail.php @@ -225,6 +225,28 @@ class Tests_Mail extends WP_UnitTestCase { $this->assertStringContainsString( $expected, $mailer->get_sent()->header ); } + /** + * Tests that wp_mail() returns false with an empty home URL and does not error out on PHP 8.1. + * + * @ticket 54730 + */ + public function test_wp_mail_with_empty_home_url() { + $to = 'address@tld.com'; + $subject = 'Testing'; + $message = 'Test Message'; + + // Multisite test runs. + add_filter( 'network_home_url', '__return_empty_string' ); + + // Single site test runs. + add_filter( 'home_url', '__return_empty_string' ); + + $success = wp_mail( $to, $subject, $message ); + + $this->assertFalse( $success, 'wp_mail() should have returned false' ); + $this->assertGreaterThan( 0, did_action( 'wp_mail_failed' ), 'wp_mail_failed action was not called' ); + } + /** * @ticket 30266 */