summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLee Rowlands2017-12-07 21:48:00 (GMT)
committerLee Rowlands2017-12-07 21:48:00 (GMT)
commit66fc18077a8338983f6aea1fe4ec6ddba7cfde70 (patch)
tree8527924bf02a4fd67546be87340850da48d374a9
parent8203805823b71d1cbafbeef5f59f153efa6b12da (diff)
Issue #2717965 by Yogesh Pawar, pguillard, alexpott, Liam Morland, skylord, oxy86, cilefen, balagan, Anthony Fok: Site name is not UTF-8 encoded in email headers
-rw-r--r--core/lib/Drupal/Component/Utility/Unicode.php7
-rw-r--r--core/lib/Drupal/Core/Mail/MailManager.php8
-rw-r--r--core/modules/system/tests/src/Functional/Mail/MailTest.php7
3 files changed, 19 insertions, 3 deletions
diff --git a/core/lib/Drupal/Component/Utility/Unicode.php b/core/lib/Drupal/Component/Utility/Unicode.php
index b222f78..f8e026f 100644
--- a/core/lib/Drupal/Component/Utility/Unicode.php
+++ b/core/lib/Drupal/Component/Utility/Unicode.php
@@ -603,11 +603,13 @@ EOD;
*
* @param string $string
* The header to encode.
+ * @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) {
+ public static function mimeHeaderEncode($string, $shorten = FALSE) {
if (preg_match('/[^\x20-\x7E]/', $string)) {
// floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
$chunk_size = 47;
@@ -616,6 +618,9 @@ EOD;
while ($len > 0) {
$chunk = static::truncateBytes($string, $chunk_size);
$output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
+ if ($shorten) {
+ break;
+ }
$c = strlen($chunk);
$string = substr($string, $c);
$len -= $c;
diff --git a/core/lib/Drupal/Core/Mail/MailManager.php b/core/lib/Drupal/Core/Mail/MailManager.php
index 6a092c9..348dcee 100644
--- a/core/lib/Drupal/Core/Mail/MailManager.php
+++ b/core/lib/Drupal/Core/Mail/MailManager.php
@@ -3,6 +3,7 @@
namespace Drupal\Core\Mail;
use Drupal\Component\Render\PlainTextOutput;
+use Drupal\Component\Utility\Unicode;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
@@ -248,7 +249,12 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface {
// Return-Path headers should have a domain authorized to use the
// originating SMTP server.
$headers['Sender'] = $headers['Return-Path'] = $site_mail;
- $headers['From'] = $site_config->get('name') . ' <' . $site_mail . '>';
+ // Headers are usually encoded in the mail plugin that implements
+ // \Drupal\Core\Mail\MailInterface::mail(), for example,
+ // \Drupal\Core\Mail\Plugin\Mail\PhpMail::mail(). The site name must be
+ // encoded here to prevent mail plugins from encoding the email address,
+ // which would break the header.
+ $headers['From'] = Unicode::mimeHeaderEncode($site_config->get('name'), TRUE) . ' <' . $site_mail . '>';
if ($reply) {
$headers['Reply-to'] = $reply;
}
diff --git a/core/modules/system/tests/src/Functional/Mail/MailTest.php b/core/modules/system/tests/src/Functional/Mail/MailTest.php
index 74549a3..e8accdd 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,11 +91,15 @@ class MailTest extends BrowserTestBase {
$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($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
+ $this->assertEquals('=?UTF-8?B?RHLDqXBhbCB0aGlzIGlzIGEgdmVyeSBsb25nIHRlc3Qgc2VudGVuY2UgdG8gdGU=?= <simpletest@example.com>', $sent_message['headers']['From'], 'From header is correctly encoded.');
+ $this->assertEquals('Drépal this is a very long test sentence to te <simpletest@example.com>', 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.');
}