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

Using a PHP mailer class over the built-in mail() function in PHP offers several advantages, including better support for attachments, HTML emails, SMTP authentication, and easier handling of multipart messages. PHP mailer classes often provide more robust error handling and debugging capabilities compared to the basic mail() function.

// Example of sending an email using PHPMailer class
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$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 HTML message body';
$mail->AltBody = 'This is the plain text version of the email';

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