What are the potential pitfalls of sending HTML emails with embedded graphics using PHP's mail function?
When sending HTML emails with embedded graphics using PHP's mail function, one potential pitfall is that some email clients may not display the embedded graphics correctly or at all. To ensure the graphics are displayed properly, you can include the graphics as attachments in the email rather than embedding them directly in the HTML.
// Set the email content type to multipart/mixed
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";
// Define the boundary
$boundary = "boundary";
// Define the message body
$message = "--$boundary\r\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "<html><body><img src=\"cid:graphic1\"></body></html>\r\n";
// Define the attachment
$message .= "--$boundary\r\n";
$message .= "Content-Type: image/jpeg\r\n";
$message .= "Content-Disposition: inline; filename=\"graphic.jpg\"\r\n";
$message .= "Content-ID: <graphic1>\r\n\r\n";
$message .= file_get_contents("path/to/graphic.jpg") . "\r\n";
// Send the email
mail($to, $subject, $message, $headers);