0 Jul 15, 2026 — https://sf.gl/2237

Embed email logos in Laravel

It's nice to embed your logo in emails. When the logo file is embedded (or attached), it is then visible immediately for users that have enabled email privacy in their email clients - without having to click the "Show Inline Images" button.

I've customized the default Laravel email template quite a bit. You can do the same by running the php artisan vendor:publish --tag=laravel-mail command – the email view files show up in your vendor/email directory for you to modify.

However! In Laravel email templates, the $message variable that's usually used to embed images as attachments isn't available.

I first tried embedding it via the <img src="data:image/png;base64..."> method, but this does not work in Gmail.

So my solution was doing my own embedding by intercepting outgoing emails through the MessageSending listener.

Here's an example.

Simply place this file in the Listeners folder, adjust filenames, and add it as a listener for the MessageSending event.

<?php

namespace App\Listeners;

use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Str;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;

class EmbedEmailLogo
{
const PLACEHOLDER = '__EMAIL_LOGO__';

public function handle(MessageSending $event): void
{
$message = $event->message;

$html = $message->getHtmlBody();

if (!$html || !Str::contains($html, self::PLACEHOLDER)) {
return;
}

$logoPath = public_path('assets/images/logotype-email.png');

if (!file_exists($logoPath)) {
return;
}

$logoPart = (new DataPart(new File($logoPath), 'logotype-email.png', 'image/png'))
->asInline();

// Replace our placeholder with the proper cid: reference
$html = str_replace(
'src="' . self::PLACEHOLDER . '"',
'src="cid:' . $logoPart->getContentId() . '"',
$html
);

$message
->addPart($logoPart)
->html($html);
}
}
EmbedEmailLogo.php

After this, I can modify my header.php file:

@props(['url'])
<tr>
<td class="header content-cell">
<a href="{{ $url }}" style="display: inline-block;">
@if (trim($slot) === 'Laravel')
<img src="https://laravel.com/img/notification-logo.png" class="logo" alt="Laravel Logo">
@else
<img class="logo" src="{{ \App\Listeners\EmbedEmailLogo::PLACEHOLDER }}" alt="Webhook.site">
@endif
</a>
</td>
</tr>
vendor/mail/html/header.blade.php

And finally, my logo always shows up in all email clients - even those with email privacy or "Block Remote Content" enabled.


Leave a Comment




Note: Your comment will be shown after it has been approved.