What are common pitfalls when sending Word documents as attachments in PHP?

Common pitfalls when sending Word documents as attachments in PHP include not setting the correct MIME type for the attachment, which can result in the document not being recognized by the recipient's email client. To solve this issue, make sure to set the MIME type to "application/msword" for Word documents.

// Set the MIME type for Word documents
$mime_type = 'application/msword';

// Attach the Word document to the email
$filename = 'document.docx';
$file_path = '/path/to/document.docx';

// Load the file content
$file_content = file_get_contents($file_path);

// Encode the file content
$encoded_content = chunk_split(base64_encode($file_content));

// Create the attachment
$attachment = "--boundary\n";
$attachment .= "Content-Type: $mime_type; name=\"$filename\"\n";
$attachment .= "Content-Transfer-Encoding: base64\n";
$attachment .= "Content-Disposition: attachment; filename=\"$filename\"\n\n";
$attachment .= $encoded_content . "\n";

// Add the attachment to the email
$message .= $attachment;