How can one send an HTML email using PHP and ensure proper encoding for the content?
When sending an HTML email using PHP, it is important to ensure that the content is properly encoded to prevent any formatting issues or security vulnerabilities. To achieve this, you can use the `mb_encode_mimeheader()` function to encode the subject line and `mb_encode_mimeheader()` or `htmlspecialchars()` function to encode the email body.
// Set the email content type to HTML
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Encode the subject line
$subject = 'Subject line';
$subject = mb_encode_mimeheader($subject, "UTF-8");
// Encode the email body
$body = '<html><body><h1>Hello, World!</h1></body></html>';
$body = mb_encode_mimeheader($body, "UTF-8");
// Send the email
$to = 'recipient@example.com';
mail($to, $subject, $body, $headers);