What is the significance of setting the Content-Type to multipart/alternative in PHP email messages?

Setting the Content-Type to multipart/alternative in PHP email messages is significant because it allows the email client to choose the best format to display the message. This is particularly useful when sending emails with both HTML and plain text versions of the content. By using multipart/alternative, the email client can display either the HTML or plain text version based on the recipient's preferences or capabilities.

// Set the Content-Type header to multipart/alternative
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: multipart/alternative; boundary="boundary_here"' . "\r\n";

// Define the boundaries for the different parts of the email
$message = "--boundary_here\r\n";
$message .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "This is the plain text version of the email.\r\n";
$message .= "\r\n--boundary_here\r\n";
$message .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "\r\n";
$message .= "<html><body><p>This is the HTML version of the email.</p></body></html>\r\n";
$message .= "\r\n--boundary_here--";

// Send the email with the defined headers and message
mail('recipient@example.com', 'Subject', $message, $headers);