What are the potential advantages of using PEAR::Mail_Mime over the mail() function in PHP for sending large quantities of emails?

When sending large quantities of emails in PHP, using the mail() function can be inefficient and unreliable due to limitations in handling attachments, formatting, and headers. PEAR::Mail_Mime provides a more robust solution by allowing for easy attachment handling, HTML email formatting, and customizable headers, making it a better choice for sending bulk emails.

<?php
require_once "Mail.php";
require_once "Mail/mime.php";

$from = "sender@example.com";
$to = "recipient@example.com";
$subject = "Test Email";
$body = "This is a test email";

$host = "smtp.example.com";
$username = "smtp_username";
$password = "smtp_password";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$mime = new Mail_mime();
$mime->setHTMLBody($body);

$smtp = Mail::factory('smtp', array(
    'host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password
));

$mail = $smtp->send($to, $mime->headers($headers), $mime->get());

if (PEAR::isError($mail)) {
    echo "Error sending email: " . $mail->getMessage();
} else {
    echo "Email sent successfully!";
}
?>