How can attachments be added to emails sent from PHP using PHPMailer compared to the mail() function?

To add attachments to emails sent from PHP using PHPMailer, you need to use the `addAttachment()` method provided by PHPMailer. This method allows you to specify the file path, name, and type of the attachment. This is a more flexible and reliable way to send emails with attachments compared to the basic `mail()` function.

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

require 'vendor/autoload.php';

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

// Set up the basic email parameters
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Email with Attachment';
$mail->Body = 'This email contains an attachment.';

// Add an attachment
$mail->addAttachment('/path/to/file.pdf', 'document.pdf');

// Send the email
if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email could not be sent.';
}