What are common pitfalls when using the mail() function in PHP to send emails with HTML content?

Common pitfalls when using the mail() function in PHP to send emails with HTML content include not setting the proper headers for HTML emails, not properly formatting the HTML content, and not handling errors effectively. To solve these issues, ensure that the Content-Type header is set to text/html, properly format the HTML content, and implement error handling to catch any issues that may arise during the email sending process.

$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "<html><body><h1>Hello, this is a test HTML email!</h1></body></html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully";
} else {
    echo "Email sending failed";
}