Explain Codes LogoExplain Codes Logo

Send email with PHPMailer - embed image in body

php
responsive-design
email-marketing
image-embedding
Anton ShumikhinbyAnton Shumikhin·Jan 16, 2025
TLDR

To embed an image directly into an email with PHPMailer, leverage AddEmbeddedImage. This method lets you attach an image to your email by assigning a unique CID, serving as a reference within your HTML content.

$mail->AddEmbeddedImage('image.jpg', 'my_image_cid', 'image_name.jpg'); $mail->Body = '<img src="cid:my_image_cid">';

For the image to display within the email body, CID matching is crucial. Namely, the CID in AddEmbeddedImage has to correspond exactly with the one in the <img> tag.

A nerdy guide to PHPMailer image embedding

Discover the key to mastering image embedding with PHPMailer. It might seem scary if you're just starting, but you'll be a pro soon. Caution! PHP coding skills required.

PHPMailer methods: A quick comparison

Level up your email wizard skills – Yes, that's a thing! Understand when to use AddEmbeddedImage() or AddAttachment().

// This is an attachment, not your inline friend $mail->AddAttachment('doggo.jpg', 'doggo');

The AddAttachment() method is a trap! It tells PHPMailer, "Hey, let's attach this image as a separate file", which means your image will not be embedded in the email body as per our goal.

// That's what you're looking for to make your email snappy $mail->AddEmbeddedImage('logo.jpg', 'my_logo', 'logo.jpg');

The AddEmbeddedImage() method, on the other hand, lets you attach your image and even adds a unique CID for referencing in the <img> tag.

Working with images: A mini how-to

From local to remote images, it all comes down to fetching and handling them correctly while scripting.

  1. A simple image saved locally? No problem:

    // Sending your logo in the email like a boss $mail->AddEmbeddedImage('path/to/logo.jpg', 'logo_cid');
  2. Dealing with an external image? Say no more:

    // Don't try this at home...just kidding, it works like a charm $imageData = file_get_contents('https://example.com/image.jpg'); $mail->addStringAttachment($imageData, 'image.jpg', 'base64', 'image/jpeg', 'inline', 'boss_image');

Ups and downs of image embedding

Beware, fellow PHPMailer padawan! Being a coding Jedi means understanding and overcoming common pitfalls:

  • Blocked Images: Add descriptive alt text and never rely solely on images.
  • Large Images: Behemoths can be flagged as spam or slow down loading. Compress before embedding.
  • Reveal the Mystery: Remember, some clients block images by default, so ensure the email offers value, even without images.

The wise old man's guide to perfect email layout

Resizing for every device

Master responsive design. Your emails will look amazing on every screen.

CSS as your sidekick

Even if the images don't load, a well-styled email with CSS will look professional. Your subscribers will thank you.

Keeping things light

Keep in mind that sending an email is not like shipping your furniture. The lighter, the better. Compress and pick your images wisely.

The final test

Test your emails on various clients and adjust if necessary. Tools are plenty, options endless, errors are... let's hope none!