diff -u b/src/Plugin/Mail/SwiftMailer.php b/src/Plugin/Mail/SwiftMailer.php --- b/src/Plugin/Mail/SwiftMailer.php +++ b/src/Plugin/Mail/SwiftMailer.php @@ -593,7 +593,7 @@ $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
. - if ($applicable_format == SWIFTMAILER_FORMAT_HTML && strlen(strip_tags($body)) === strlen($body)) { + 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
and converts URLs to links. $build = [ only in patch2: unchanged: --- /dev/null +++ b/tests/src/Kernel/Plugin/Mail/SwiftMailerTest.php @@ -0,0 +1,81 @@ +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('

Lorem ipsum dolor sit amet

'), + Markup::create('

consetetur sadipscing elitr

'), + Markup::create('

sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat

'), + Markup::create('

sed diam voluptua.

'), + ], + ], + 'expected' => "

Lorem ipsum dolor sit amet

\n

consetetur sadipscing elitr

\n

sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat

\n

sed diam voluptua.

", + ], + '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' => "

Lorem ipsum dolor sit amet
\nconsetetur sadipscing elitr
\nsed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat
\nsed diam voluptua.

\n", + ], + ]; + } + +}