What are the differences between using multipart/alternative and multipart/mixed for sending HTML emails with images in PHP?
When sending HTML emails with images in PHP, it is important to choose the correct MIME type for the email content. Using multipart/alternative allows you to send the same content in different formats (e.g., HTML and plain text), while multipart/mixed is used when including attachments or images in the email. Therefore, when sending HTML emails with images, it is recommended to use multipart/mixed to ensure the images are properly displayed in the email.
$to = 'recipient@example.com';
$subject = 'HTML Email with Images';
$message = '<html><body><h1>Hello, world!</h1><p>This is an HTML email with images:</p><img src="image.jpg" alt="Image"></body></html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: multipart/mixed; boundary="boundary"' . "\r\n";
$headers .= '--boundary' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 7bit' . "\r\n";
$headers .= $message . "\r\n";
$headers .= '--boundary' . "\r\n";
$headers .= 'Content-Type: image/jpeg; name="image.jpg"' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64' . "\r\n";
$headers .= 'Content-Disposition: inline; filename="image.jpg"' . "\r\n";
$headers .= chunk_split(base64_encode(file_get_contents('image.jpg'))) . "\r\n";
$headers .= '--boundary--' . "\r\n";
mail($to, $subject, '', $headers);