Is using a PHP mailer class like PHPMailer a best practice for sending emails with multiple attachments?

When sending emails with multiple attachments in PHP, using a dedicated mailer class like PHPMailer is considered a best practice. PHPMailer provides a more robust and reliable way to send emails with attachments, handling encoding, security, and error handling more effectively than the built-in mail() function.

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

$mail = new PHPMailer\PHPMailer\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->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'Email body';

// Attach multiple files
$mail->addAttachment('path/to/file1.pdf', 'File1.pdf');
$mail->addAttachment('path/to/file2.jpg', 'File2.jpg');

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