What are the best practices for structuring and formatting HTML emails in PHP, including handling line breaks and special characters?
When sending HTML emails in PHP, it is important to properly structure and format the content to ensure it displays correctly in email clients. This includes using HTML tags for formatting, handling line breaks with <br> tags, and encoding special characters using htmlentities() function.
<?php
// Set the content type and charset
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Encode special characters
$message = htmlentities($message);
// Add line breaks
$message = nl2br($message);
// Send the email
mail($to, $subject, $message, $headers);
?>