From 5374ce0bca35d07ca4b89f969dd81396528e98e4 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Fri, 15 Jun 2018 11:12:49 +0200 Subject: [PATCH] Dont append base path for url --- src/ImageEmbedPlugin.php | 8 ++++++- tests/EmbedImagePluginTest.php | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/ImageEmbedPlugin.php b/src/ImageEmbedPlugin.php index 3562e3f..f3fed79 100644 --- a/src/ImageEmbedPlugin.php +++ b/src/ImageEmbedPlugin.php @@ -72,7 +72,13 @@ protected function embedImages(Swift_Mime_SimpleMessage $message, Swift_Mime_Sim */ if (strpos($src, 'cid:') === false) { - $entity = \Swift_Image::fromPath($this->basePath . $src); + $path = $src; + + if (filter_var($src, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === false) { + $path = $this->basePath . $src; + } + + $entity = \Swift_Image::fromPath($path); $message->setChildren( array_merge($message->getChildren(), [$entity]) ); diff --git a/tests/EmbedImagePluginTest.php b/tests/EmbedImagePluginTest.php index 08c2e7f..6c82539 100644 --- a/tests/EmbedImagePluginTest.php +++ b/tests/EmbedImagePluginTest.php @@ -111,6 +111,49 @@ public function testAttachment() ); } + public function testBasePathAndUrl() + { + $this->mailer = new Swift_Mailer(new Swift_NullTransport()); + $this->mailer->registerPlugin(new ImageEmbedPlugin(__DIR__)); + + $message = $this->createMessage(); + + $html = << + + +

some text

+ localPath + url + + +HTML; + $html = sprintf( + $html, + '/fixtures/placeholder.png', + 'https://github.com/Hexanet/swiftmailer-image-embed/raw/master/tests/fixtures/placeholder.png' + ); + + $message->setBody($html, 'text/html'); + + $this->mailer->send($message); + + $children = $message->getChildren(); + + $this->assertInstanceOf('\Swift_Image', $children[0], 'Local image is embedded in the message'); + $this->assertContains( + sprintf('localPath', $children[0]->getId()), + $message->getBody(), + 'Image is linked in body' + ); + $this->assertInstanceOf('\Swift_Image', $children[1], 'Remote image is embedded in the message'); + $this->assertContains( + sprintf('url', $children[1]->getId()), + $message->getBody(), + 'Image is linked in body' + ); + } + /** * @return Swift_Message */