Skip to content

Commit 81f3dba

Browse files
pniapstaylorotwell
andauthored
[10.x] Render mailable inline images (#48292)
* Render mailable inline images * Fix code style * Fix code style * formatting * formatting * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 21968a1 commit 81f3dba

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/Illuminate/Mail/Mailer.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,38 @@ public function render($view, array $data = [])
253253

254254
$data['message'] = $this->createMessage();
255255

256-
return $this->renderView($view ?: $plain, $data);
256+
return $this->replaceEmbeddedAttachments(
257+
$this->renderView($view ?: $plain, $data),
258+
$data['message']->getSymfonyMessage()->getAttachments()
259+
);
260+
}
261+
262+
/**
263+
* Replace the embedded image attachments with raw, inline image data for browser rendering.
264+
*
265+
* @param string $renderedView
266+
* @param array $attachments
267+
* @return string
268+
*/
269+
protected function replaceEmbeddedAttachments(string $renderedView, array $attachments)
270+
{
271+
if (preg_match_all('/<img.+?src=[\'"]cid:([^\'"]+)[\'"].*?>/i', $renderedView, $matches)) {
272+
foreach (array_unique($matches[1]) as $image) {
273+
foreach ($attachments as $attachment) {
274+
if ($attachment->getFilename() === $image) {
275+
$renderedView = str_replace(
276+
'cid:'.$image,
277+
'data:'.$attachment->getContentType().';base64,'.$attachment->bodyToString(),
278+
$renderedView
279+
);
280+
281+
break;
282+
}
283+
}
284+
}
285+
}
286+
287+
return $renderedView;
257288
}
258289

259290
/**

0 commit comments

Comments
 (0)