Commit 5ac45f0d authored by Lucas Hedding's avatar Lucas Hedding Committed by Florian Weber
Browse files

Issue #2831959 by heddn, Boymix81, webflo: HTML in Drupal core emails

parent 4beca5eb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
format: text/plain
filter_format: plain_text
respect_format: true
convert_mode: false
character_set: UTF-8
+3 −0
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@ swiftmailer.message:
    format:
      type: string
      label: 'Message format'
    filter_format:
      type: string
      label: 'Filter format'
    respect_format:
      type: boolean
      label: 'Respect format'
+15 −1
Original line number Diff line number Diff line
@@ -588,7 +588,21 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface {
    // separated by the mail line endings. Keep Markup objects and escape others
    // and then treat the result as safe markup.
    $line_endings = Settings::get('mail_line_endings', PHP_EOL);
    $message['body'] = Markup::create(implode($line_endings, array_map(function ($body) {
    $applicable_format = $this->getApplicableFormat($message);
    $filter_format = $this->config['message']['filter_format'];
    $message['body'] = Markup::create(implode($line_endings, array_map(function ($body) use ($applicable_format, $filter_format) {
      // If the body contains no html tags but the applicable format is HTML,
      // we can assume newlines will need be converted to <br>.
      if ($applicable_format == SWIFTMAILER_FORMAT_HTML && Unicode::strlen(strip_tags($body)) === Unicode::strlen($body)) {
        // The default fallback format is 'plain_text', which escapes markup,
        // converts new lines to <br> and converts URLs to links.
        $build = [
          '#type' => 'processed_text',
          '#text' => $body,
          '#format' => $filter_format,
        ];
        $body = $this->renderer->renderPlain($build);
      }
      // If $item is not marked safe then it will be escaped.
      return $body instanceof MarkupInterface ? $body : Html::escape($body);
    }, $message['body'])));
+9 −0
Original line number Diff line number Diff line
@@ -32,3 +32,12 @@ function swiftmailer_install() {
    'sender' => 'swiftmailer',
  ])->save();
}

/**
 * Set default filter_format for HTML emails with no HTML markup.
 */
function swiftmailer_update_8101() {
 Drupal::configFactory()->getEditable('swiftmailer.message')
   ->set('filter_format', 'plain_text')
   ->save();
}
+84 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\swiftmailer\Kernel\Plugin\Mail;

use Drupal\Core\Render\Markup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\swiftmailer\Utility\Conversion;

/**
 * @coversDefaultClass \Drupal\swiftmailer\Plugin\Mail\SwiftMailer
 * @group swiftmailer
 */
class SwiftMailerTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'user',
    'filter',
    'swiftmailer',
  ];

  /**
   * The swiftmailer plugin.
   *
   * @var \Drupal\swiftmailer\Plugin\Mail\SwiftMailer
   */
  protected $plugin;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->installConfig([
      'swiftmailer',
      'filter',
    ]);
    $this->installEntitySchema('user');
    $this->installSchema('user', 'users_data');
    $this->plugin = $this->container->get('plugin.manager.mail')
      ->createInstance('swiftmailer');
  }

  /**
   * Tests massaging the message body.
   *
   * @dataProvider bodyDataProvider
   */
  public function testMassageMessageBody(array $message, $expected) {
    $message['params']['format'] = SWIFTMAILER_FORMAT_HTML;
    $actual = $this->plugin->massageMessageBody($message);
    $this->assertSame($expected, (string) $actual['body']);
  }

  /**
   * Data provider of body data with markup and without HTML markup.
   */
  public function bodyDataProvider() {
    return [
      'with html' => [
        'message' => [
          'body' => [
            Markup::create('<p>Lorem ipsum dolor sit amet</p>'),
            Markup::create('<p>consetetur sadipscing elitr</p>'),
            Markup::create('<p>sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat</p>'),
            Markup::create('<p>sed diam voluptua.</p>'),
          ],
        ],
        'expected' => "<p>Lorem ipsum dolor sit amet</p>\n<p>consetetur sadipscing elitr</p>\n<p>sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat</p>\n<p>sed diam voluptua.</p>",
      ],
      'no html' => [
        'message' => [
          'body' => [
            "Lorem ipsum dolor sit amet\nconsetetur sadipscing elitr\nsed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat\nsed diam voluptua.",
          ],
        ],
        'expected' => "<p>Lorem ipsum dolor sit amet<br />\nconsetetur sadipscing elitr<br />\nsed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat<br />\nsed diam voluptua.</p>\n",
      ],
    ];
  }

}