What are the best practices for sending HTML emails with embedded images using PHP?

When sending HTML emails with embedded images using PHP, it's important to ensure that the images are properly encoded and included in the email content as inline attachments. This can be achieved by encoding the image data using base64 encoding and embedding it within the HTML content of the email.

// Path to the image file
$image_path = 'path/to/image.jpg';

// Read image data
$image_data = file_get_contents($image_path);

// Encode image data
$encoded_image = base64_encode($image_data);

// Define the HTML content with embedded image
$html_content = '<html><body><h1>Embedded Image</h1><img src="data:image/jpeg;base64,'. $encoded_image .'"></body></html>';

// Set headers for HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Send email
$to = 'recipient@example.com';
$subject = 'HTML Email with Embedded Image';
mail($to, $subject, $html_content, $headers);