What are the advantages of using the phpMailer class over the mail() function in PHP for sending emails?

Using the phpMailer class over the mail() function in PHP for sending emails offers several advantages such as better error handling, support for attachments, HTML emails, SMTP authentication, and easier configuration of email settings.

<?php
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/Exception.php';

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

// Set up SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

// Set email content
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Testing phpMailer';
$mail->Body = 'This is a test email sent using phpMailer';

// Send the email
if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Error sending email: ' . $mail->ErrorInfo;
}