What are the potential pitfalls of not properly handling HTML emails in PHP?

Potential pitfalls of not properly handling HTML emails in PHP include emails not displaying correctly in the recipient's inbox, security vulnerabilities like cross-site scripting (XSS) attacks, and potential for emails to be marked as spam. To properly handle HTML emails in PHP, use a library like PHPMailer to ensure emails are formatted correctly and securely.

// Example using PHPMailer to send HTML email
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    $mail->isHTML(true);
    $mail->Subject = 'HTML email test';
    $mail->Body = '<h1>Hello, this is a test email!</h1>';

    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo 'Email could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}