Loading core/lib/Drupal/Core/Mail/MailInterface.php +14 −6 Original line number Diff line number Diff line Loading @@ -15,12 +15,20 @@ interface MailInterface { * Formats a message prior to sending. * * Allows to preprocess, format, and postprocess a mail message before it is * passed to the sending system. By default, all messages may contain HTML and * are converted to plain-text by the Drupal\Core\Mail\Plugin\Mail\PhpMail * implementation. For example, an alternative implementation could override * the default implementation and also sanitize the HTML for usage in a MIME- * encoded email, but still invoking the Drupal\Core\Mail\Plugin\Mail\PhpMail * implementation to generate an alternate plain-text version for sending. * passed to the sending system. The message body is received as an array of * lines that are either strings or objects implementing * \Drupal\Component\Render\MarkupInterface. It must be converted to the * format expected by mail() which is a single string that can be either * plain text or HTML. In the HTML case an alternate plain-text version can * be returned in $message['plain']. * * The conversion process consists of the following steps: * - If the output is HTML then convert any input line that is a string using * \Drupal\Component\Utility\Html\Html::Escape(). * - If the output is plain text then convert any input line that is markup * using \Drupal\Core\Mail\MailFormatHelper::htmlToText(). * - Join the input lines into a single string. * - Wrap long lines using \Drupal\Core\Mail\MailFormatHelper::wrapMail(). * * @param array $message * A message array, as described in hook_mail_alter(). Loading core/modules/contact/src/MessageViewBuilder.php +0 −4 Original line number Diff line number Diff line Loading @@ -4,7 +4,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Render\Element; /** Loading Loading @@ -40,9 +39,6 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N $build[$key]['#label_display'] = 'hidden'; } } $build['#post_render'][] = function ($html, array $elements) { return MailFormatHelper::htmlToText($html); }; } return $build; } Loading core/modules/contact/tests/src/Functional/ContactPersonalTest.php +18 −3 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Utility\Html; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Test\AssertMailTrait; use Drupal\Tests\BrowserTestBase; Loading @@ -25,7 +26,7 @@ class ContactPersonalTest extends BrowserTestBase { * * @var array */ protected static $modules = ['contact', 'dblog']; protected static $modules = ['contact', 'dblog', 'mail_html_test']; /** * {@inheritdoc} Loading Loading @@ -116,6 +117,20 @@ public function testSendPersonalContactMessage() { $this->assertRaw(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders)); // Ensure an unescaped version of the email does not exist anywhere. $this->assertNoRaw($this->webUser->getEmail()); // Test HTML mails. $mail_config = $this->config('system.mail'); $mail_config->set('interface.default', 'test_html_mail_collector'); $mail_config->save(); $this->drupalLogin($this->webUser); $message['message[0][value]'] = 'This <i>is</i> a more <b>specific</b> <sup>test</sup>, the emails are formatted now.'; $message = $this->submitPersonalContact($this->contactUser, $message); // Assert mail content. $this->assertMailString('body', 'Hello ' . $variables['@recipient-name'], 1); $this->assertMailString('body', $this->webUser->getDisplayName(), 1); $this->assertMailString('body', Html::Escape($message['message[0][value]']), 1); } /** Loading Loading @@ -326,8 +341,8 @@ protected function checkContactAccess($response, $contact_value = NULL) { */ protected function submitPersonalContact(AccountInterface $account, array $message = []) { $message += [ 'subject[0][value]' => $this->randomMachineName(16), 'message[0][value]' => $this->randomMachineName(64), 'subject[0][value]' => $this->randomMachineName(16) . '< " =+ >', 'message[0][value]' => $this->randomMachineName(64) . '< " =+ >', ]; $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message')); return $message; Loading Loading
core/lib/Drupal/Core/Mail/MailInterface.php +14 −6 Original line number Diff line number Diff line Loading @@ -15,12 +15,20 @@ interface MailInterface { * Formats a message prior to sending. * * Allows to preprocess, format, and postprocess a mail message before it is * passed to the sending system. By default, all messages may contain HTML and * are converted to plain-text by the Drupal\Core\Mail\Plugin\Mail\PhpMail * implementation. For example, an alternative implementation could override * the default implementation and also sanitize the HTML for usage in a MIME- * encoded email, but still invoking the Drupal\Core\Mail\Plugin\Mail\PhpMail * implementation to generate an alternate plain-text version for sending. * passed to the sending system. The message body is received as an array of * lines that are either strings or objects implementing * \Drupal\Component\Render\MarkupInterface. It must be converted to the * format expected by mail() which is a single string that can be either * plain text or HTML. In the HTML case an alternate plain-text version can * be returned in $message['plain']. * * The conversion process consists of the following steps: * - If the output is HTML then convert any input line that is a string using * \Drupal\Component\Utility\Html\Html::Escape(). * - If the output is plain text then convert any input line that is markup * using \Drupal\Core\Mail\MailFormatHelper::htmlToText(). * - Join the input lines into a single string. * - Wrap long lines using \Drupal\Core\Mail\MailFormatHelper::wrapMail(). * * @param array $message * A message array, as described in hook_mail_alter(). Loading
core/modules/contact/src/MessageViewBuilder.php +0 −4 Original line number Diff line number Diff line Loading @@ -4,7 +4,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Render\Element; /** Loading Loading @@ -40,9 +39,6 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N $build[$key]['#label_display'] = 'hidden'; } } $build['#post_render'][] = function ($html, array $elements) { return MailFormatHelper::htmlToText($html); }; } return $build; } Loading
core/modules/contact/tests/src/Functional/ContactPersonalTest.php +18 −3 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Utility\Html; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Test\AssertMailTrait; use Drupal\Tests\BrowserTestBase; Loading @@ -25,7 +26,7 @@ class ContactPersonalTest extends BrowserTestBase { * * @var array */ protected static $modules = ['contact', 'dblog']; protected static $modules = ['contact', 'dblog', 'mail_html_test']; /** * {@inheritdoc} Loading Loading @@ -116,6 +117,20 @@ public function testSendPersonalContactMessage() { $this->assertRaw(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders)); // Ensure an unescaped version of the email does not exist anywhere. $this->assertNoRaw($this->webUser->getEmail()); // Test HTML mails. $mail_config = $this->config('system.mail'); $mail_config->set('interface.default', 'test_html_mail_collector'); $mail_config->save(); $this->drupalLogin($this->webUser); $message['message[0][value]'] = 'This <i>is</i> a more <b>specific</b> <sup>test</sup>, the emails are formatted now.'; $message = $this->submitPersonalContact($this->contactUser, $message); // Assert mail content. $this->assertMailString('body', 'Hello ' . $variables['@recipient-name'], 1); $this->assertMailString('body', $this->webUser->getDisplayName(), 1); $this->assertMailString('body', Html::Escape($message['message[0][value]']), 1); } /** Loading Loading @@ -326,8 +341,8 @@ protected function checkContactAccess($response, $contact_value = NULL) { */ protected function submitPersonalContact(AccountInterface $account, array $message = []) { $message += [ 'subject[0][value]' => $this->randomMachineName(16), 'message[0][value]' => $this->randomMachineName(64), 'subject[0][value]' => $this->randomMachineName(16) . '< " =+ >', 'message[0][value]' => $this->randomMachineName(64) . '< " =+ >', ]; $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message')); return $message; Loading