Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #18 from ter-informatique/basepath-and-url
Browse files Browse the repository at this point in the history
Dont append base path to complete URLs
  • Loading branch information
Martin Damien authored Jun 15, 2018
2 parents d3e1f4b + 5374ce0 commit b86c6f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ImageEmbedPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
);
Expand Down
43 changes: 43 additions & 0 deletions tests/EmbedImagePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<HTML
<html>
<head></head>
<body>
<p>some text</p>
<img src="%s" alt="localPath">
<img src="%s" alt="url">
</body>
</html>
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('<img src="cid:%s" alt="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('<img src="cid:%s" alt="url">', $children[1]->getId()),
$message->getBody(),
'Image is linked in body'
);
}

/**
* @return Swift_Message
*/
Expand Down

0 comments on commit b86c6f1

Please sign in to comment.