What are multipart-mails and what should be considered when using them in PHP?

Multipart emails are emails that contain both plain text and HTML content, allowing the recipient's email client to choose which version to display. When using multipart emails in PHP, it is important to ensure that both the plain text and HTML versions are properly formatted and included in the email headers.

$to = "recipient@example.com";
$subject = "Multipart Email Test";

$text_message = "This is the plain text version of the email.";
$html_message = "<p>This is the HTML version of the email.</p>";

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

$multipart_message = "Content-Type: multipart/alternative; boundary=\"boundary\"\r\n";
$multipart_message .= "\r\n--boundary\r\n";
$multipart_message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$multipart_message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$multipart_message .= $text_message . "\r\n";
$multipart_message .= "\r\n--boundary\r\n";
$multipart_message .= "Content-Type: text/html; charset=UTF-8\r\n";
$multipart_message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$multipart_message .= $html_message . "\r\n";
$multipart_message .= "\r\n--boundary--";

mail($to, $subject, $multipart_message, $headers);