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';
}
?>
Related Questions
- How can file paths and file existence be properly checked when using include in PHP?
- What are the potential drawbacks of storing player information in separate columns in a database table?
- Are there any specific PHP frameworks or libraries that are well-suited for implementing a basic user management and shopping system like the one described in the forum thread?