What are the potential pitfalls of using the mail() function in PHP to send HTML emails?

One potential pitfall of using the mail() function in PHP to send HTML emails is that it does not handle headers properly, which can result in emails being marked as spam by email clients. To solve this issue, you can use the PHPMailer library, which provides a more robust and reliable way to send HTML emails with proper headers.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    $mail->isHTML(true);
    $mail->Subject = 'Subject';
    $mail->Body = 'HTML content here';
    $mail->addAddress('recipient@example.com');
    
    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}