HTML emails are a richer and more personalized form of email, allowing the inclusion of images, links, and styles. Embedded images refer to images that are included directly as part of the email content, rather than as attachments.
In PHP, we can use PHPMAILER to send HTML emails with embedded images. PHPMAILER is a powerful PHP mail library that allows you to send various types of emails easily.
Here is an example of using PHPMAILER to send an HTML email with an embedded image:
<?php require 'PHPMailer/PHPMailerAutoload.php'; // Create a new PHPMailer object $mail = new PHPMailer; // Configure email server settings $mail->isSMTP(); // Set email to use SMTP $mail->Host = 'smtp.example.com'; // Email server address $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'your-email@example.com'; // SMTP username $mail->Password = 'password'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption $mail->Port = 587; // Email server port // Configure sender information $mail->setFrom('your-email@example.com', 'Your Name'); // Sender's email and name // Configure recipient information $mail->addAddress('recipient@example.com', 'Recipient Name'); // Recipient's email and name // Configure email content $mail->Subject = 'My HTML Email'; // Email subject $mail->isHTML(true); // Set email format to HTML // HTML content in the email, including an embedded image $mail->Body = '<h1>Hello, World!</h1> <p>This is an HTML email with an embedded image.</p> <p><span>Learn more</span> “<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP Free Learning Notes (Deep Dive)</a>”;</p> <p>@@##@@</p>'; // Add embedded image attachment $mail->AddEmbeddedImage('path/to/image.jpg', 'logo'); // Image path and name // Send email if ($mail->send()) { echo 'Email sent successfully'; } else { echo 'Email sending failed: ' . $mail->ErrorInfo; }
In the code example above, we first include the PHPMAILER library with a require statement and create a new PHPMailer object. Then, we configure the properties of the $mail object to set up the email server, sender, recipient, and email content.
Within the HTML content of the email, we use an tag to reference the embedded image, where the src attribute points to the embedded image using the "cid:logo" identifier. We then use the $mail->AddEmbeddedImage() method to attach the actual image file as an embedded image, where the first parameter is the image path and name, and the second parameter is the CID identifier, which should match the one used in the email content.
Finally, we use the $mail->send() method to send the email, and output a success or failure message based on the result.
Sending HTML emails with embedded images using PHP and PHPMAILER is relatively simple. By using the AddEmbeddedImage() method to add an embedded image attachment and referencing the corresponding CID identifier in the email content, we can easily create more engaging and personalized HTML emails. Using PHPMAILER allows you to enhance the appeal and effectiveness of your emails with minimal effort.