How to embed images in html email

How to embed images in html email

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Embed Images in HTML Email

Sending an image directly from a server URL often results in broken icons or blank spaces when recipients view your email. This happens because most modern email clients, particularly Outlook on Windows and various mobile providers, block external content by default for security reasons. To ensure the image renders correctly, you must embed it using Base64 encoding so the data travels inline with the email body rather than as an external resource.

The core principle is to convert your image file into a long string of ASCII characters. This process removes the dependency on any specific server path or domain. When the email client parses the message, it doesn't need to fetch an image; instead, it decodes the Base64 string directly into a rendered picture.

The Base64 Approach

To implement this in PHP, you typically use the phpseclib library or a custom function to handle the conversion. While many libraries exist, understanding the underlying logic helps when troubleshooting encoding issues.

Here is a practical example using a standard approach often found in Laravel applications:

use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;

function embedImage($path) {
    // Read the file content and convert to Base64
    $base64 = base64_encode(file_get_contents($path));
    
    // Wrap the string in data URI scheme
    return 'data:' . mime_content_type($path) . ';base64,' . $base64;
}

// Usage within your view or mail builder
$emailContent = view('emails.invoice', [
    'imageUrl' => embedImage(public_path('assets/logo.png')),
]);

Mail::to($user)->send(new InvoiceEmail($emailContent));

When you inspect the final HTML sent via SMTP, the <img> tag should look like this:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." width="200" />

Notice there are no spaces around the equals signs in the src attribute. If your image is large, the Base64 string will be long, but this ensures compatibility across all clients without relying on the recipient's server settings.

Handling Attachments vs. Embedded Images

A common pitfall is confusing email attachments with embedded images. An attachment requires the client to download a file before viewing it, which is often blocked by corporate firewalls or user preferences. An embedded image renders instantly upon opening the message. If your goal is brand consistency or visual storytelling, embedding is strictly superior. However, if you are sending sensitive data that needs to be downloaded and verified later, an attachment might still be necessary.

Performance and Size Considerations

While Base64 encoding solves rendering issues, it does increase the total size of the email message. Every image embedded in this manner adds its full weight to the MIME payload. This can lead to longer delivery times for bulk campaigns or hit limits on certain mail servers that cap message size at 25KB or similar thresholds.

If you are sending a newsletter with multiple images, embedding them all individually can bloat the message significantly. A better strategy is to optimize your source images using tools like pngquant or gifsicle before encoding them. Compressing the original file reduces the Base64 string length proportionally.

Alternative: CID Headers

If you are dealing with very large assets or want to avoid inflating the message size, consider using Content-ID (CID) headers. This method involves uploading images to a storage bucket and referencing them via a unique ID within the HTML. The email client then attempts to fetch these resources. However, this approach re-introduces the risk of external content blocking. Many users configure their clients to block remote images entirely, rendering your design invisible unless they manually click "display blocked images."

Best Practices for Implementation

  1. Optimize First: Always compress images before encoding them. A 50KB image becomes larger in Base64 format, but a 10KB optimized image keeps the email lightweight.
  2. Use Inline Styles: When embedding images, ensure your HTML structure relies on inline CSS. Many clients strip out <style> blocks from external sheets or embedded stylesheets.
  3. Fallback Text: Always provide an alt attribute for every embedded image. If the client blocks content or fails to decode the Base64 string, the user should still understand the context of the missing visual element.

For developers working specifically within the Laravel ecosystem, leveraging the built-in mailer components alongside these encoding techniques ensures your emails look professional regardless of the recipient's environment.

Our network