How can PHP be used to send HTML emails with images attached?
To send HTML emails with images attached using PHP, you can use the PHPMailer library. This library allows you to easily send emails with HTML content and attach images. You will need to set the email content as HTML, embed the images using their CID (Content-ID), and attach the images to the email.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('your@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'HTML Email with Image Attachment';
$mail->Body = '<p>This is an HTML email with an image attached.</p><img src="cid:logo">';
$mail->addEmbeddedImage('path/to/image.jpg', 'logo');
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo 'Email could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}
?>