Skip to content

Commit

Permalink
[10.x] Render mailable inline images (#48292)
Browse files Browse the repository at this point in the history
* Render mailable inline images

* Fix code style

* Fix code style

* formatting

* formatting

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
pniaps and taylorotwell authored Sep 12, 2023
1 parent 21968a1 commit 81f3dba
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,38 @@ public function render($view, array $data = [])

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

return $this->renderView($view ?: $plain, $data);
return $this->replaceEmbeddedAttachments(
$this->renderView($view ?: $plain, $data),
$data['message']->getSymfonyMessage()->getAttachments()
);
}

/**
* Replace the embedded image attachments with raw, inline image data for browser rendering.
*
* @param string $renderedView
* @param array $attachments
* @return string
*/
protected function replaceEmbeddedAttachments(string $renderedView, array $attachments)
{
if (preg_match_all('/<img.+?src=[\'"]cid:([^\'"]+)[\'"].*?>/i', $renderedView, $matches)) {
foreach (array_unique($matches[1]) as $image) {
foreach ($attachments as $attachment) {
if ($attachment->getFilename() === $image) {
$renderedView = str_replace(
'cid:'.$image,
'data:'.$attachment->getContentType().';base64,'.$attachment->bodyToString(),
$renderedView
);

break;
}
}
}
}

return $renderedView;
}

/**
Expand Down

0 comments on commit 81f3dba

Please sign in to comment.