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

merge-requests/25/head
catch 2020-10-09 10:17:19 +01:00
parent 1e03b1c66d
commit 945d76a119
3 changed files with 32 additions and 13 deletions

View File

@ -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().

View File

@ -4,7 +4,6 @@ namespace Drupal\contact;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Render\Element;
/**
@ -40,9 +39,6 @@ class MessageViewBuilder extends EntityViewBuilder {
$build[$key]['#label_display'] = 'hidden';
}
}
$build['#post_render'][] = function ($html, array $elements) {
return MailFormatHelper::htmlToText($html);
};
}
return $build;
}

View File

@ -4,6 +4,7 @@ namespace Drupal\Tests\contact\Functional;
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;
@ -25,7 +26,7 @@ class ContactPersonalTest extends BrowserTestBase {
*
* @var array
*/
protected static $modules = ['contact', 'dblog'];
protected static $modules = ['contact', 'dblog', 'mail_html_test'];
/**
* {@inheritdoc}
@ -116,6 +117,20 @@ class ContactPersonalTest extends BrowserTestBase {
$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);
}
/**
@ -327,8 +342,8 @@ class ContactPersonalTest extends BrowserTestBase {
*/
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;