Are there any specific libraries or packages in PHP that can assist with adding attachments to emails?

To add attachments to emails in PHP, you can use the built-in PHPMailer library. PHPMailer provides a simple and efficient way to send emails with attachments. You can easily include attachments by specifying the file path and name in the addAttachment() method.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

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

// Set up the basic 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;

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

// Set the email details
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Email with Attachments';
$mail->Body = 'Please see the attached files.';

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