What potential pitfalls should be considered when including file attachments in PHP emails?

One potential pitfall when including file attachments in PHP emails is the risk of file size limitations causing the attachment to not be properly sent. To solve this issue, you can check the file size before attaching it to the email and handle any files that exceed the limit accordingly.

// Check file size before attaching to email
$maxFileSize = 5 * 1024 * 1024; // 5MB
$attachment = $_FILES['file']['tmp_name'];

if (filesize($attachment) <= $maxFileSize) {
    // Attach file to email
    $fileContent = file_get_contents($attachment);
    $fileType = mime_content_type($attachment);
    $fileName = basename($attachment);

    $mail->addStringAttachment($fileContent, $fileName, 'base64', $fileType);
} else {
    // Handle file size limit exceeded
    echo "File size limit exceeded. Please upload a smaller file.";
}