What are the best practices for sending HTML emails using the mail() function in PHP?

When sending HTML emails using the mail() function in PHP, it is important to set the appropriate headers to indicate that the email content is HTML formatted. This can be achieved by setting the "Content-Type" header to "text/html". Additionally, make sure to properly format the HTML content of the email within the message body parameter of the mail() function.

$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "<html><body><h1>Hello, this is a test HTML email!</h1></body></html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: sender@example.com" . "\r\n";

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