Are there any best practices for formatting HTML content in emails to prevent it from being displayed as text in certain scenarios?

When sending HTML content in emails, it's important to include a plain text version as well to ensure that the email can still be read if the recipient's email client doesn't support HTML. To prevent the HTML content from being displayed as text in certain scenarios, you can use a multipart MIME format to include both HTML and plain text versions of the email.

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

$html_content = '<html><body><h1>Hello, world!</h1></body></html>';
$text_content = 'Hello, world!';

$boundary = uniqid('np');

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

$message = "This is a MIME encoded message.";
$message .= "\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain;charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $text_content;
$message .= "\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html;charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $html_content;
$message .= "\r\n--" . $boundary . "--";

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