What are the advantages of using a PHP mailer class like phpMailer over the built-in mail() function?

When sending emails in PHP, using a PHP mailer class like phpMailer offers several advantages over the built-in mail() function. phpMailer provides a more robust and secure way to send emails, with features such as SMTP authentication, HTML email support, file attachments, and more. Additionally, phpMailer simplifies the process of sending emails by handling many of the complexities of email sending for you.

// Include the phpMailer class file
require 'path/to/PHPMailerAutoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer;

// Set up the email parameters
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject of the Email';
$mail->Body = 'This is the body of the email';

// Send the email
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}