Current Location: Home> Latest Articles> How to Embed Images and Attachments in Emails Using PHP? Tutorial & Examples

How to Embed Images and Attachments in Emails Using PHP? Tutorial & Examples

M66 2025-06-25

How to Embed Images and Attachments in Emails Using PHP?

Email has become an essential part of modern communication. To improve the visual appeal and information delivery, there are times when we need to embed images or attachments in emails. This article will show you how to achieve this functionality in PHP using the third-party library PHPMailer.

1. Install the PHPMailer Library

First, you need to install the PHPMailer library. You can do this by running the following command via Composer:

composer require phpmailer/phpmailer

2. Create a PHPMailer Instance and Set Up Email Information

Next, in your PHP code, you'll need to create a PHPMailer instance and configure the relevant email details, such as the recipient, sender, subject, etc.

require 'vendor/autoload.php'; // Include the PHPMailer library

$mailer = new PHPMailer\PHPMailer\PHPMailer(); // Create the PHPMailer instance

// Configure SMTP
$mailer->isSMTP();
$mailer->Host = 'smtp.example.com';
$mailer->SMTPAuth = true;
$mailer->Username = 'you@example.com';
$mailer->Password = 'your-password';
$mailer->Port = 587;
$mailer->CharSet = 'UTF-8';

// Set up the email information
$mailer->setFrom('you@example.com', 'Your Name');
$mailer->addAddress('recipient@example.com', 'Recipient Name');
$mailer->Subject = 'Email Test: Image and Attachment';
$mailer->Body = 'This is an email with an image and an attachment.';
$mailer->AltBody = 'This is the plain text version of the email.';

Note that the SMTP configuration should be adjusted according to the email provider you are using.

3. Add Embedded Images

To add an image to the email, we use the `addEmbeddedImage()` method to embed the image file as a resource, and refer to it in the email body using the Content-ID (CID).

// Add an embedded image
$mailer->addEmbeddedImage('/path/to/image.jpg', 'logo', 'logo.jpg');

// Refer to the image in the email body using CID
$mailer->Body .= '<p><img src="cid:logo" alt="Logo"></p>';

In the code above, `/path/to/image.jpg` is the actual path to the image file, `logo` is the reference name for the image resource, and `logo.jpg` is the filename the image will display as in the email.

4. Add Attachments

If you need to add attachments, use the `addAttachment()` method to attach files to the email.

// Add an attachment
$mailer->addAttachment('/path/to/file.pdf', 'document.pdf');

Here, `/path/to/file.pdf` is the actual path to the attachment, and `document.pdf` is the filename the attachment will display as in the email.

5. Send the Email

Once everything is ready, you can send the email by calling the `send()` method.

// Send the email
if ($mailer->send()) {
    echo 'Email sent successfully!';
} else {
    echo 'Email sending failed: ' . $mailer->ErrorInfo;
}

Now you have successfully learned how to embed images and attachments in emails using PHPMailer in PHP. By following these steps, you can enhance the visual and informational quality of your emails, providing a better experience for the recipient.

In real-world usage, you may need to modify configurations based on your specific needs. I hope this article has been helpful and that it empowers you to use PHP for email sending more effectively!