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

Commit b86c6f1

Browse files
author
Martin Damien
authored
Merge pull request #18 from ter-informatique/basepath-and-url
Dont append base path to complete URLs
2 parents d3e1f4b + 5374ce0 commit b86c6f1

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/ImageEmbedPlugin.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ protected function embedImages(Swift_Mime_SimpleMessage $message, Swift_Mime_Sim
7272
*/
7373
if (strpos($src, 'cid:') === false) {
7474

75-
$entity = \Swift_Image::fromPath($this->basePath . $src);
75+
$path = $src;
76+
77+
if (filter_var($src, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === false) {
78+
$path = $this->basePath . $src;
79+
}
80+
81+
$entity = \Swift_Image::fromPath($path);
7682
$message->setChildren(
7783
array_merge($message->getChildren(), [$entity])
7884
);

tests/EmbedImagePluginTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,49 @@ public function testAttachment()
111111
);
112112
}
113113

114+
public function testBasePathAndUrl()
115+
{
116+
$this->mailer = new Swift_Mailer(new Swift_NullTransport());
117+
$this->mailer->registerPlugin(new ImageEmbedPlugin(__DIR__));
118+
119+
$message = $this->createMessage();
120+
121+
$html = <<<HTML
122+
<html>
123+
<head></head>
124+
<body>
125+
<p>some text</p>
126+
<img src="%s" alt="localPath">
127+
<img src="%s" alt="url">
128+
</body>
129+
</html>
130+
HTML;
131+
$html = sprintf(
132+
$html,
133+
'/fixtures/placeholder.png',
134+
'https://github.com/Hexanet/swiftmailer-image-embed/raw/master/tests/fixtures/placeholder.png'
135+
);
136+
137+
$message->setBody($html, 'text/html');
138+
139+
$this->mailer->send($message);
140+
141+
$children = $message->getChildren();
142+
143+
$this->assertInstanceOf('\Swift_Image', $children[0], 'Local image is embedded in the message');
144+
$this->assertContains(
145+
sprintf('<img src="cid:%s" alt="localPath">', $children[0]->getId()),
146+
$message->getBody(),
147+
'Image is linked in body'
148+
);
149+
$this->assertInstanceOf('\Swift_Image', $children[1], 'Remote image is embedded in the message');
150+
$this->assertContains(
151+
sprintf('<img src="cid:%s" alt="url">', $children[1]->getId()),
152+
$message->getBody(),
153+
'Image is linked in body'
154+
);
155+
}
156+
114157
/**
115158
* @return Swift_Message
116159
*/

0 commit comments

Comments
 (0)