What are the best practices for encoding attachments with MIME and setting the correct headers in PHP?

When encoding attachments with MIME in PHP, it is important to set the correct headers to ensure that the attachment is properly formatted and can be correctly interpreted by email clients. This involves specifying the content type, encoding, and disposition of the attachment. Below is an example PHP code snippet that demonstrates how to encode an attachment with MIME and set the correct headers:

<?php

// Define the attachment file path
$attachmentFilePath = 'path/to/attachment.pdf';

// Get the file content
$fileContent = file_get_contents($attachmentFilePath);

// Encode the file content
$encodedFileContent = chunk_split(base64_encode($fileContent));

// Set the MIME headers
$mimeHeaders = [
    'Content-Type: application/pdf',
    'Content-Disposition: attachment; filename="attachment.pdf"',
    'Content-Transfer-Encoding: base64',
    'Content-ID: <attachment>',
];

// Output the headers
foreach ($mimeHeaders as $header) {
    header($header);
}

// Output the encoded file content
echo $encodedFileContent;