Skip to content
Snippets Groups Projects
Commit c2a885fa authored by catch's avatar catch
Browse files

Issue #2223967 by jofitz, LKS90, AdamPS, sja112, shaktik, pminf, Berdir,...

Issue #2223967 by jofitz, LKS90, AdamPS, sja112, shaktik, pminf, Berdir, mxr576, bmcclure, Manuel Garcia, naveenvalecha, Gogowitsch, andypost, alexpott, jonathanshaw, claudiu.cristea, greggles: Do not decode a contact message twice

(cherry picked from commit 945d76a1)
parent 81b76e31
No related branches found
No related tags found
No related merge requests found
...@@ -15,12 +15,20 @@ interface MailInterface { ...@@ -15,12 +15,20 @@ interface MailInterface {
* Formats a message prior to sending. * Formats a message prior to sending.
* *
* Allows to preprocess, format, and postprocess a mail message before it is * 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 * passed to the sending system. The message body is received as an array of
* are converted to plain-text by the Drupal\Core\Mail\Plugin\Mail\PhpMail * lines that are either strings or objects implementing
* implementation. For example, an alternative implementation could override * \Drupal\Component\Render\MarkupInterface. It must be converted to the
* the default implementation and also sanitize the HTML for usage in a MIME- * format expected by mail() which is a single string that can be either
* encoded email, but still invoking the Drupal\Core\Mail\Plugin\Mail\PhpMail * plain text or HTML. In the HTML case an alternate plain-text version can
* implementation to generate an alternate plain-text version for sending. * 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 * @param array $message
* A message array, as described in hook_mail_alter(). * A message array, as described in hook_mail_alter().
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Render\Element; use Drupal\Core\Render\Element;
/** /**
...@@ -40,9 +39,6 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N ...@@ -40,9 +39,6 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N
$build[$key]['#label_display'] = 'hidden'; $build[$key]['#label_display'] = 'hidden';
} }
} }
$build['#post_render'][] = function ($html, array $elements) {
return MailFormatHelper::htmlToText($html);
};
} }
return $build; return $build;
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Html;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Test\AssertMailTrait; use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
...@@ -25,7 +26,7 @@ class ContactPersonalTest extends BrowserTestBase { ...@@ -25,7 +26,7 @@ class ContactPersonalTest extends BrowserTestBase {
* *
* @var array * @var array
*/ */
protected static $modules = ['contact', 'dblog']; protected static $modules = ['contact', 'dblog', 'mail_html_test'];
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -116,6 +117,20 @@ public function testSendPersonalContactMessage() { ...@@ -116,6 +117,20 @@ public function testSendPersonalContactMessage() {
$this->assertRaw(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders)); $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. // Ensure an unescaped version of the email does not exist anywhere.
$this->assertNoRaw($this->webUser->getEmail()); $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);
} }
/** /**
...@@ -326,8 +341,8 @@ protected function checkContactAccess($response, $contact_value = NULL) { ...@@ -326,8 +341,8 @@ protected function checkContactAccess($response, $contact_value = NULL) {
*/ */
protected function submitPersonalContact(AccountInterface $account, array $message = []) { protected function submitPersonalContact(AccountInterface $account, array $message = []) {
$message += [ $message += [
'subject[0][value]' => $this->randomMachineName(16), 'subject[0][value]' => $this->randomMachineName(16) . '< " =+ >',
'message[0][value]' => $this->randomMachineName(64), 'message[0][value]' => $this->randomMachineName(64) . '< " =+ >',
]; ];
$this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message')); $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message'));
return $message; return $message;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment