How does MIME mail play a role in sending HTML-formatted emails through PHP?

When sending HTML-formatted emails through PHP, MIME mail plays a crucial role in properly formatting the email content. By using MIME (Multipurpose Internet Mail Extensions), PHP can encode the HTML content and set the appropriate headers to ensure the email is correctly interpreted by the recipient's email client.

<?php
$to = 'recipient@example.com';
$subject = 'HTML Email Test';

$message = '<html><body>';
$message .= '<h1>Hello, this is a test email!</h1>';
$message .= '<p>This is an example of sending an HTML-formatted email through PHP.</p>';
$message .= '</body></html>';

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

// Additional headers
$headers .= 'From: sender@example.com' . "\r\n";

// Send the email
mail($to, $subject, $message, $headers);
?>