2026-07-15

Send email with PHPMailer - embed image in body

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Send email with PHPMailer - embed image in body

Sending Rich HTML Emails with Images using PHPMailer: The Embedded Image Solution

Sending rich, visually appealing emails often involves embedding images directly into the HTML body rather than relying solely on file attachments. If you are using PHPMailer and find that your <img> tags do not display the image, even when the image is attached separately, you have encountered a common hurdle related to how email clients handle MIME (Multipurpose Internet Mail Extensions).

As a senior developer, I can tell you that the issue isn't typically about file paths; it’s about correctly structuring the email as a multipart message so that the image data is properly embedded within the email stream rather than just attached as a separate file.

This post will walk you through the correct methodology for embedding images directly into your HTML email body using PHPMailer.

Understanding the Email Image Dilemma

When you use methods like AddAttachment(), you are telling the mail server to attach a separate file (like a JPEG or PNG) to the message. This is useful if the recipient needs to download the image separately.

However, when you want an image to appear directly within the HTML content of the email, the email must be structured using MIME parts. The image data itself needs to be encoded and inserted into the email structure so that the email client can render it inline. Simply referencing a local file path in your HTML (e.g., <img src="file:///path/to/image.jpg">) will almost always fail because email clients cannot access local server files directly for embedding purposes.

The solution lies in using PHPMailer’s built-in functions designed specifically to handle this MIME encoding.

The Correct Approach: Embedding Images with PHPMailer

To successfully embed an image, you must use the AddEmbeddedImage() method provided by PHPMailer. This method handles reading the image file and correctly packaging it into the email as a separate part of the message, ensuring the HTML body references the embedded content correctly.

Step-by-Step Implementation

Here is a practical example demonstrating how to embed an image within an HTML email using PHP and PHPMailer.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings setup (SMTP configuration omitted for brevity)
    $mail->isSMTP();
    $mail->Host       = 'smtp.example.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'user@example.com';
    $mail->Password   = 'your_password';
    $mail->setFrom('sender@example.com', 'Mailer');
    $mail->addAddress('recipient@example.com');

    // 1. Set the email content (HTML)
    $htmlBody = "<h1>Hello, this is an embedded image test!</h1>" .
                "<p>This image was successfully embedded directly into the email body.</p>" .
                "<img src='cid:my_embedded_image'>" . // Crucial: Reference the Content ID (CID)
                "<p>The rest of the email content follows...</p>";

    $mail->isHTML(true);
    $mail->msgHTML($htmlBody);

    // 2. Embed the image using AddEmbeddedImage()
    // The first argument is the path to the image file on your server.
    // The second argument (CID) is the unique identifier you will reference in the HTML.
    $imagePath = 'path/to/your/image.jpg';
    $cid = 'my_embedded_image';

    $mail->AddEmbeddedImage($imagePath, $cid, 'image.jpg');

    // 3. Send the email
    $mail->send();
    echo 'Message has been sent successfully.';

} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

Key Takeaways from the Code

  1. AddEmbeddedImage($path, $cid, $filename): This is the function that performs the necessary MIME encoding. It takes your local file path, assigns it a unique Content ID ($cid), and tells PHPMailer how to handle the attachment structure.
  2. src='cid:my_embedded_image': This is the most critical part for display. In your HTML <img> tag's src attribute, you must use the cid: prefix followed by the Content ID you specified in the previous step. This tells the email client, "Load the image data associated with this specific Content ID within the email structure."

Best Practices and Context

When dealing with complex mail structures like embedding images, it is important to remember that robust application architecture pays off. Frameworks designed for building scalable applications, such as those found in the Laravel ecosystem, emphasize clean separation of concerns, which extends to data handling even in simple tasks like email composition. Relying on well-tested libraries ensures you avoid common pitfalls related to MIME encoding and security.

Always ensure that the file path provided to AddEmbeddedImage() is accessible by your PHP script and that the image file itself has the correct MIME type permissions set up on your web server. By mastering these embedded techniques, you move beyond simple attachments to create truly rich and engaging email communications.

Conclusion

The difficulty in embedding images with PHPMailer stems from the difference between attaching a file and embedding data within the MIME structure. By correctly utilizing AddEmbeddedImage() and referencing the image via its Content ID (cid:), you instruct the mail client exactly how to render the visual content directly into the HTML body. This approach provides a superior user experience compared to relying on separate attachments, making your emails significantly more professional.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.