How can one ensure that HTML code in emails is properly rendered when using PHP's mail() function?
When using PHP's mail() function to send HTML emails, it is important to set the appropriate headers to ensure that the HTML code is properly rendered by the email client. This can be achieved by setting the Content-Type header to "text/html". Additionally, make sure to properly format the HTML content within the message body of the email.
$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "<html><body><h1>Hello, World!</h1><p>This is a test email.</p></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);