diff --git a/core/lib/Drupal/Component/Utility/Unicode.php b/core/lib/Drupal/Component/Utility/Unicode.php index 80861494f9..d11e2101f4 100644 --- a/core/lib/Drupal/Component/Utility/Unicode.php +++ b/core/lib/Drupal/Component/Utility/Unicode.php @@ -603,13 +603,13 @@ public static function strcasecmp($str1 , $str2) { * * @param string $string * The header to encode. - * @param bool $first_chunk_only + * @param bool $shorten * If TRUE, only return the first chunk of a multi-chunk encoded string. * * @return string * The mime-encoded header. */ - public static function mimeHeaderEncode($string, $first_chunk_only = FALSE) { + public static function mimeHeaderEncode($string, $shorten = FALSE) { if (preg_match('/[^\x20-\x7E]/', $string)) { $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75); $len = strlen($string); @@ -617,7 +617,7 @@ public static function mimeHeaderEncode($string, $first_chunk_only = FALSE) { while ($len > 0) { $chunk = static::truncateBytes($string, $chunk_size); $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n"; - if ($first_chunk_only) { + if ($shorten) { break; } $c = strlen($chunk); diff --git a/core/modules/system/tests/src/Functional/Mail/MailTest.php b/core/modules/system/tests/src/Functional/Mail/MailTest.php index b39707111f..e8accddb6f 100644 --- a/core/modules/system/tests/src/Functional/Mail/MailTest.php +++ b/core/modules/system/tests/src/Functional/Mail/MailTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\system\Functional\Mail; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Mail\Plugin\Mail\TestMailCollector; use Drupal\Tests\BrowserTestBase; use Drupal\system_mail_failure_test\Plugin\Mail\TestPhpMailFailure; @@ -90,12 +91,15 @@ public function testFromAndReplyToHeader() { $this->assertEqual($reply_email, $sent_message['headers']['Reply-to'], 'Message reply-to headers are set.'); $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); + // Test that long site names containing characters that need MIME encoding + // works as expected. $this->config('system.site')->set('name', 'Drépal this is a very long test sentence to test what happens with very long site names')->save(); // Send an email and check that the From-header contains the site name. \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language); $captured_emails = \Drupal::state()->get('system.test_mail_collector'); $sent_message = end($captured_emails); - $this->assertEqual('=?UTF-8?B?RHLDqXBhbCB0aGlzIGlzIGEgdmVyeSBsb25nIHRlc3Qgc2VudGVuY2UgdG8gdGU=?= ', $sent_message['headers']['From'], 'From header is correctly encoded.'); + $this->assertEquals('=?UTF-8?B?RHLDqXBhbCB0aGlzIGlzIGEgdmVyeSBsb25nIHRlc3Qgc2VudGVuY2UgdG8gdGU=?= ', $sent_message['headers']['From'], 'From header is correctly encoded.'); + $this->assertEquals('Drépal this is a very long test sentence to te ', Unicode::mimeHeaderDecode($sent_message['headers']['From']), 'From header is correctly encoded.'); $this->assertFalse(isset($sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.'); $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); }