Skip to content

Commit 941ea7c

Browse files
committed
Support absolute paths in setHTMLBody for automatic CID embedding
1 parent 5f16f76 commit 941ea7c

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/Mail/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ public function setHtmlBody(string $html, ?string $basePath = null): static
207207
|<body[^<>]*\s background\s*=\s*
208208
|<[^<>]+\s style\s*=\s* ["\'][^"\'>]+[:\s] url\(
209209
|<style[^>]*>[^<]+ [:\s] url\()
210-
(["\']?)(?![a-z]+:|[/\#])([^"\'>)\s]+)
210+
(["\']?)(?![a-z]+:|[\#])([^"\'>)\s]+)
211211
|\[\[ ([\w()+./@~-]+) \]\]
212212
#ix',
213213
captureOffset: true,
214214
);
215215
foreach (array_reverse($matches) as $m) {
216-
$file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
216+
$file = rtrim($basePath, '/\\') . '/' . ltrim(isset($m[4]) ? $m[4][0] : urldecode($m[3][0]), '/\\');
217217
if (!isset($cids[$file])) {
218218
$cids[$file] = substr($this->addEmbeddedFile($file)->getHeader('Content-ID'), 1, -1);
219219
}
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
@@ -38,8 +38,6 @@ $mailer->send($mail);
3838

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

41-
42-
4341
$mail = new Message;
4442
$mail->setHTMLBody("
4543
<a href='test.php?src=SOME'>some link</a>
@@ -53,3 +51,23 @@ $mailer = new TestMailer;
5351
$mailer->send($mail);
5452

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

0 commit comments

Comments
 (0)