How can the formatting of HTML emails in PHP be optimized to prevent the email content from displaying as source code?
When sending HTML emails in PHP, it's important to properly set the Content-Type header to ensure the email content is displayed correctly in the recipient's inbox. To prevent the email content from displaying as source code, you should set the Content-Type header to "text/html" and use the correct HTML formatting for the email content.
<?php
$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "
<html>
<head>
<title>HTML Email</title>
</head>
<body>
<h1>Hello, this is a test HTML email!</h1>
<p>This is a paragraph in the email body.</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Send email
mail($to, $subject, $message, $headers);
?>