What are some best practices for handling HTML emails with embedded images in PHP using pear mime mail?

When sending HTML emails with embedded images in PHP using PEAR MIME Mail, it is important to use the CID (Content-ID) method to reference the images within the HTML content. This ensures that the images are displayed correctly when the email is received by the recipient.

<?php

require_once "Mail.php";
require_once "Mail/mime.php";

$from = "sender@example.com";
$to = "recipient@example.com";
$subject = "HTML Email with Embedded Image";

$html = '<html><body>';
$html .= '<p>Here is an embedded image:</p>';
$html .= '<img src="cid:unique_cid_here" alt="Embedded Image">';
$html .= '</body></html>';

$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);

$image = '/path/to/image.jpg';
$mime->addHTMLImage($image, 'image/jpeg', basename($image), true, 'unique_cid_here');

$body = $mime->get();
$headers = $mime->headers(array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject,
    'Content-Type' => 'multipart/related; boundary="' . $mime->boundary() . '"'
));

$mail = Mail::factory('smtp', array('host' => 'smtp.example.com', 'auth' => true, 'username' => 'username', 'password' => 'password'));

$mail->send($to, $headers, $body);

?>