Are there any recommended PHP libraries or classes, such as PHPmailer or Swiftmailer, that could improve the handling of email attachments in this scenario?

To improve the handling of email attachments in PHP, it is recommended to use a library like PHPMailer or SwiftMailer. These libraries provide easy-to-use methods for adding attachments to emails and handling the sending process efficiently. By utilizing these libraries, you can ensure that your email attachments are properly included and sent without any issues.

<?php
require 'vendor/autoload.php'; // Include the library file

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

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

// Set the sender and recipient
$mail->setFrom('your@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');

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

// Set the email subject and body
$mail->Subject = 'Email with Attachments';
$mail->Body = 'Please find attached the files you requested.';

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