What are the best practices for handling attachments in emails using PHP's Mail_Mime package?

When sending emails with attachments using PHP's Mail_Mime package, it is important to properly handle the attachments to ensure they are included correctly in the email. This can be achieved by specifying the file path, MIME type, and encoding for each attachment. Additionally, it is recommended to set the appropriate headers for the email to ensure compatibility with various email clients.

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

$from = 'sender@example.com';
$to = 'recipient@example.com';
$subject = 'Email with Attachment';
$body = 'Please see the attached file.';
$attachment = '/path/to/attachment.pdf';
$mime = new Mail_mime();

$mime->setTXTBody($body);
$mime->addAttachment($attachment, 'application/pdf');

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

$body = $mime->get();
$headers = $mime->headers($headers);

$mail = Mail::factory('mail');
$mail->send($to, $headers, $body);
?>