How can PHP Mailer be effectively utilized for sending HTML emails with embedded images?
To send HTML emails with embedded images using PHP Mailer, you need to include the images as attachments and reference them in the HTML content using the appropriate cid (Content-ID) value. This ensures that the images are displayed correctly when the email is opened by the recipient.
<?php
require 'vendor/autoload.php';
// Create a new PHPMailer instance
$mail = new PHPMailer();
// Set the email parameters
$mail->isHTML(true);
$mail->Subject = 'HTML Email with Embedded Image';
$mail->Body = '<img src="cid:image1">';
// Attach the image file
$mail->AddEmbeddedImage('path/to/image.jpg', 'image1', 'image.jpg');
// Set the sender and recipient
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Send the email
if($mail->send()) {
echo 'Email sent successfully';
} else {
echo 'Email sending failed';
}
?>