What best practices should be followed when sending HTML emails using PHP?

When sending HTML emails using PHP, it is important to ensure that the email is properly formatted and includes necessary headers such as Content-Type and MIME-Version. Additionally, it is recommended to use a library like PHPMailer to handle the email sending process, as it provides built-in methods for sending HTML emails securely.

<?php
require 'vendor/autoload.php'; // Include PHPMailer library

// Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();

// Set up the email parameters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject of the email';
$mail->Body = '<h1>This is the HTML content of the email</h1>';
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Send the email
if($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email could not be sent';
}