Skip to content

Commit d356e6e

Browse files
authored
Support absolute paths in setHTMLBody for automatic CID embedding (#103)
1 parent b2bf71d commit d356e6e

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

src/Mail/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ public function setHtmlBody(string $html, ?string $basePath = null): static
197197
|<body[^<>]*\s background\s*=\s*
198198
|<[^<>]+\s style\s*=\s* ["\'][^"\'>]+[:\s] url\(
199199
|<style[^>]*>[^<]+ [:\s] url\()
200-
(["\']?)(?![a-z]+:|[/\#])([^"\'>)\s]+)
200+
(["\']?)(?![a-z]+:|[\#])([^"\'>)\s]+)
201201
|\[\[ ([\w()+./@~-]+) \]\]
202202
#ix',
203203
captureOffset: true,
204204
);
205205
foreach (array_reverse($matches) as $m) {
206-
$file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
206+
$file = rtrim($basePath, '/\\') . '/' . ltrim(isset($m[4]) ? $m[4][0] : urldecode($m[3][0]), '/\\');
207207
if (!isset($cids[$file])) {
208208
$contentId = $this->addEmbeddedFile($file)->getHeader('Content-ID');
209209
$cids[$file] = is_string($contentId) ? substr($contentId, 1, -1) : '';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
MIME-Version: 1.0
2+
X-Mailer: Nette Framework
3+
Date: %a%
4+
From: John Doe <doe@example.com>
5+
To: Lady Jane <jane@example.com>
6+
Subject: Hello Jane!
7+
Message-ID: <%S%@%S%>
8+
Content-Type: multipart/alternative;
9+
boundary="--------%S%"
10+
11+
----------%S%
12+
Content-Type: text/plain; charset=UTF-8
13+
Content-Transfer-Encoding: 7bit
14+
15+
Sample text
16+
----------%S%
17+
Content-Type: multipart/related;
18+
boundary="--------%S%"
19+
20+
----------%S%
21+
Content-Type: text/html; charset=UTF-8
22+
Content-Transfer-Encoding: 7bit
23+
24+
<BODY id=1 background="cid:%S%">
25+
<img src="cid:%S%">
26+
<div title=a style="background:url('cid:%S%')">
27+
<style type=text/css>body { background: url('cid:%S%') } </style>
28+
cid:%S%
29+
30+
----------%S%
31+
Content-Type: image/png
32+
Content-Transfer-Encoding: base64
33+
Content-Disposition: inline; filename="background.png"
34+
Content-ID: <%S%>
35+
36+
iVBORw0KGgoAAAANSUhEUgAABAAAAAAGCAMAAABq1Ry/AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
37+
bWFnZVJlYWR5ccllPAAAADlQTFRFIYzeKYzeMZTeOZTeQpTeQpzeSpzeUpzeY6Xee73nhL3nlMbn
38+
nMbnrc7nvdbvxt7v1ufv5+fv7+/vqVk59gAAAKFJREFUeNrsmMEKgzAQRHcTq63WJPr/H9ska4vQ
39+
2rOG5+CEgT0JPiYrqiKiJue723Afp+ccQlqWFSHUtqQCQCoFVF0BwCMDYI4p8XUQal3269ubAeB7
40+
A0CI1gBWDMMattIA1BBQAdC9G0DcrgAfVuwPDMOaMHGqtgEo5o8bwDcKiETi1aP4QoDtyQWgtyXg
41+
bgdwzIDfxeBfYphhhk81/BJgAHfwneqeqMofAAAAAElFTkSuQmCC
42+
----------%S%--
43+
----------%S%--

tests/Mail/Mail.textualAndHtmlBody.embedded.phpt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ $mailer->send($mail);
3636

3737
Assert::matchFile(__DIR__ . '/Mail.textualAndHtmlBody.embedded.expect', TestMailer::$output);
3838

39-
40-
4139
$mail = new Message;
4240
$mail->setHTMLBody("
4341
<a href='test.php?src=SOME'>some link</a>
@@ -51,3 +49,23 @@ $mailer = new TestMailer;
5149
$mailer->send($mail);
5250

5351
Assert::matchFile(__DIR__ . '/Mail.textualAndHtmlBody.embedded2.expect', TestMailer::$output);
52+
53+
$mail = new Message;
54+
55+
$mail->setFrom('John Doe <doe@example.com>');
56+
$mail->addTo('Lady Jane <jane@example.com>');
57+
$mail->setSubject('Hello Jane!');
58+
59+
$mail->setBody('Sample text');
60+
$mail->setHTMLBody('
61+
<BODY id=1 background="/background.png">
62+
<img src="/backgroun%64.png">
63+
<div title=a style="background:url(\'/background.png\')">
64+
<style type=text/css>body { background: url(\'/background.png\') } </style>
65+
[[background.png]]
66+
', __DIR__ . '/fixtures');
67+
// append automatically $mail->addEmbeddedFile('files/background.png');
68+
69+
$mailer = new TestMailer;
70+
$mailer->send($mail);
71+
Assert::matchFile(__DIR__ . '/Mail.textualAndHtmlBody.embedded.expect3', $mailer::$output);

0 commit comments

Comments
 (0)