From 96843822c9e6fba3173730cc32c6fd5efeb439b3 Mon Sep 17 00:00:00 2001 From: catch Date: Sat, 26 Sep 2015 08:39:34 +0100 Subject: [PATCH] Issue #2572597 by alexpott, bircher: Replace !placeholder with @placeholder in mail code --- core/lib/Drupal/Core/Mail/MailInterface.php | 3 +- core/lib/Drupal/Core/Mail/MailManager.php | 7 ++++ .../Drupal/Core/Mail/MailManagerInterface.php | 4 +-- .../Drupal/Core/Mail/Plugin/Mail/PhpMail.php | 1 + core/modules/contact/contact.module | 32 +++++++++---------- core/modules/contact/src/MailHandler.php | 2 +- core/modules/update/update.module | 12 +++---- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/core/lib/Drupal/Core/Mail/MailInterface.php b/core/lib/Drupal/Core/Mail/MailInterface.php index 3042a4d192a..aaaa2b58d65 100644 --- a/core/lib/Drupal/Core/Mail/MailInterface.php +++ b/core/lib/Drupal/Core/Mail/MailInterface.php @@ -53,7 +53,8 @@ interface MailInterface { * - User * - User , Another User * - subject: Subject of the email to be sent. This must not contain any - * newline characters, or the mail may not be sent properly. + * newline characters, or the mail may not be sent properly. The subject + * is converted to plain text by the mail plugin manager. * - body: Message to be sent. Accepts both CRLF and LF line-endings. * Email bodies must be wrapped. For smart plain text wrapping you can use * \Drupal\Core\Mail\MailFormatHelper::wrapMail() . diff --git a/core/lib/Drupal/Core/Mail/MailManager.php b/core/lib/Drupal/Core/Mail/MailManager.php index 1e7bdc65a3e..22b9a2810e1 100644 --- a/core/lib/Drupal/Core/Mail/MailManager.php +++ b/core/lib/Drupal/Core/Mail/MailManager.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Mail; +use Drupal\Component\Utility\PlainTextOutput; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Cache\CacheBackendInterface; @@ -222,6 +223,12 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface { } // Sending was originally requested and was not canceled. else { + // Ensure that subject is plain text. By default translated and + // formatted strings are prepared for the HTML context and email + // subjects are plain strings. + if ($message['subject']) { + $message['subject'] = PlainTextOutput::renderFromHtml($message['subject']); + } $message['result'] = $system->mail($message); // Log errors. if (!$message['result']) { diff --git a/core/lib/Drupal/Core/Mail/MailManagerInterface.php b/core/lib/Drupal/Core/Mail/MailManagerInterface.php index ded236b7570..196726ede5a 100644 --- a/core/lib/Drupal/Core/Mail/MailManagerInterface.php +++ b/core/lib/Drupal/Core/Mail/MailManagerInterface.php @@ -67,8 +67,8 @@ interface MailManagerInterface extends PluginManagerInterface { * $message['send'] = FALSE; * break; * } - * $message['subject'] = t('Notification from !site', $variables, $options); - * $message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, $options); + * $message['subject'] = t('Notification from @site', $variables, $options); + * $message['body'][] = t("Dear @username\n\nThere is new content available on the site.", $variables, $options); * break; * } * } diff --git a/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php index c9410bf9a06..9dcb582da46 100644 --- a/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php +++ b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php @@ -35,6 +35,7 @@ class PhpMail implements MailInterface { public function format(array $message) { // Join the body array into one string. $message['body'] = implode("\n\n", $message['body']); + // Convert any HTML to plain-text. $message['body'] = MailFormatHelper::htmlToText($message['body']); // Wrap the mail body for sending. diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index bebc1413ca1..a3e47b3b393 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -122,17 +122,17 @@ function contact_mail($key, &$message, $params) { $language = \Drupal::languageManager()->getLanguage($message['langcode']); $variables = array( - '!site-name' => \Drupal::config('system.site')->get('name'), - '!subject' => $contact_message->getSubject(), - '!form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL, - '!form-url' => \Drupal::url('', [], ['absolute' => TRUE, 'language' => $language]), - '!sender-name' => $sender->getDisplayName(), + '@site-name' => \Drupal::config('system.site')->get('name'), + '@subject' => $contact_message->getSubject(), + '@form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL, + '@form-url' => \Drupal::url('', [], ['absolute' => TRUE, 'language' => $language]), + '@sender-name' => $sender->getDisplayName(), ); if ($sender->isAuthenticated()) { - $variables['!sender-url'] = $sender->url('canonical', array('absolute' => TRUE, 'language' => $language)); + $variables['@sender-url'] = $sender->url('canonical', array('absolute' => TRUE, 'language' => $language)); } else { - $variables['!sender-url'] = $params['sender']->getEmail(); + $variables['@sender-url'] = $params['sender']->getEmail(); } $options = array('langcode' => $language->getId()); @@ -140,27 +140,27 @@ function contact_mail($key, &$message, $params) { switch ($key) { case 'page_mail': case 'page_copy': - $message['subject'] .= t('[!form] !subject', $variables, $options); - $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, $options); + $message['subject'] .= t('[@form] @subject', $variables, $options); + $message['body'][] = t("@sender-name (@sender-url) sent a message using the contact form at @form-url.", $variables, $options); $build = entity_view($contact_message, 'mail', $language->getId()); $message['body'][] = (string) \Drupal::service('renderer')->renderPlain($build); break; case 'page_autoreply': - $message['subject'] .= t('[!form] !subject', $variables, $options); + $message['subject'] .= t('[@form] @subject', $variables, $options); $message['body'][] = $params['contact_form']->getReply(); break; case 'user_mail': case 'user_copy': $variables += array( - '!recipient-name' => $params['recipient']->getDisplayName(), - '!recipient-edit-url' => $params['recipient']->url('edit-form', array('absolute' => TRUE, 'language' => $language)), + '@recipient-name' => $params['recipient']->getDisplayName(), + '@recipient-edit-url' => $params['recipient']->url('edit-form', array('absolute' => TRUE, 'language' => $language)), ); - $message['subject'] .= t('[!site-name] !subject', $variables, $options); - $message['body'][] = t('Hello !recipient-name,', $variables, $options); - $message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form at !site-name.", $variables, $options); - $message['body'][] = t("If you don't want to receive such emails, you can change your settings at !recipient-edit-url.", $variables, $options); + $message['subject'] .= t('[@site-name] @subject', $variables, $options); + $message['body'][] = t('Hello @recipient-name,', $variables, $options); + $message['body'][] = t("@sender-name (@sender-url) has sent you a message via your contact form at @site-name.", $variables, $options); + $message['body'][] = t("If you don't want to receive such emails, you can change your settings at @recipient-edit-url.", $variables, $options); $build = entity_view($contact_message, 'mail', $language->getId()); $message['body'][] = (string) \Drupal::service('renderer')->renderPlain($build); break; diff --git a/core/modules/contact/src/MailHandler.php b/core/modules/contact/src/MailHandler.php index 308acf58867..606292cbc6d 100644 --- a/core/modules/contact/src/MailHandler.php +++ b/core/modules/contact/src/MailHandler.php @@ -91,7 +91,7 @@ class MailHandler implements MailHandlerInterface { // For the email message, clarify that the sender name is not verified; it // could potentially clash with a username on this site. - $sender_cloned->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName())); + $sender_cloned->name = $this->t('@name (not verified)', array('@name' => $message->getSenderName())); } // Build email parameters. diff --git a/core/modules/update/update.module b/core/modules/update/update.module index 060c56d2fab..1742d96a1a0 100644 --- a/core/modules/update/update.module +++ b/core/modules/update/update.module @@ -446,7 +446,7 @@ function update_fetch_data_finished($success, $results) { function update_mail($key, &$message, $params) { $langcode = $message['langcode']; $language = \Drupal::languageManager()->getLanguage($langcode); - $message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => \Drupal::config('system.site')->get('name')), array('langcode' => $langcode)); + $message['subject'] .= t('New release(s) available for @site_name', array('@site_name' => \Drupal::config('system.site')->get('name')), array('langcode' => $langcode)); foreach ($params as $msg_type => $msg_reason) { $message['body'][] = _update_message_text($msg_type, $msg_reason, $langcode); } @@ -456,10 +456,10 @@ function update_mail($key, &$message, $params) { } $settings_url = \Drupal::url('update.settings', [], ['absolute' => TRUE]); if (\Drupal::config('update.settings')->get('notification.threshold') == 'all') { - $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, !url.', array('!url' => $settings_url)); + $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, @url.', array('@url' => $settings_url)); } else { - $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, !url.', array('!url' => $settings_url)); + $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, @url.', array('@url' => $settings_url)); } } @@ -628,9 +628,9 @@ function update_verify_update_archive($project, $archive_file, $directory) { elseif (!$compatible_project) { $errors[] = \Drupal::translation()->formatPlural( count($incompatible), - '%archive_file contains a version of %names that is not compatible with Drupal !version.', - '%archive_file contains versions of modules or themes that are not compatible with Drupal !version: %names', - array('!version' => \Drupal::CORE_COMPATIBILITY, '%archive_file' => drupal_basename($archive_file), '%names' => implode(', ', $incompatible)) + '%archive_file contains a version of %names that is not compatible with Drupal @version.', + '%archive_file contains versions of modules or themes that are not compatible with Drupal @version: %names', + array('@version' => \Drupal::CORE_COMPATIBILITY, '%archive_file' => drupal_basename($archive_file), '%names' => implode(', ', $incompatible)) ); }