Page 1 of 1

PHP mail - Word 2007 MIME problem

Posted: Wed Sep 22, 2010 3:26 pm
by thePan5
I have developed an online application in which the user can upload their resume. It works just fine if the resume is in Word 2003, txt, rtf or pdf.

If the resume is in Word 2007, I get a "the file cannot be opened because there are problems with the contents." >>The file is corrupt and cannot be opened. If I click OK, I get "Word found unreadable content in file.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes. When I click yes, the resume will actually open okay. But I would like to eliminate the error.

The application page validates upon submission and if it passes then it includes resumeupload.php and then pdf_process.php.

Resumeupload.php checks and allows only .doc, .docx, .pdf, .txt, .wks and .rtf files. If this passes then this code is executed:

Code: Select all

$rtemp = $_FILES["re_resume"]["tmp_name"];
$rsize = $_FILES["re_resume"]["size"];
$rtype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; //This is usually a variable, but I'll set it to Word 2007 to test

//open the resume for attachment
$fp2 = fopen($rtemp, "rb"); 
$resume = fread($fp2, $rsize); 
$resume = chunk_split(file_get_contents(base64_encode($rtemp)));
fclose($fp2);
The top part of pdf_process.php deals with the application (pdf) which works fine, so we can ignore it:

Code: Select all

        $fileattname = $filename; //application
        $fileatttype = "application/vnd.adobe.fdf"; //application 
        $data=chunk_split(base64_encode($fdf)); //application
        $mime_boundary = '==Multipart_Boundary_x'.md5(time()).'x'; 
        $headers = "From: $from\n".
            "MIME-Version: 1.0\n".
            "Content-Type: multipart/mixed;\n".
            " boundary=\"{$mime_boundary}\"";
        $message = "This is a multi-part message in MIME format.\n\n".
            "--{$mime_boundary}\n".
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
            "Content-Transfer-Encoding: 7bit\n\n".
            $message."\n\n".
            "--{$mime_boundary}\n".
            "Content-Type: {$rtype};\n". //MIME type set to Word 2007.  I can also set it here... still doesn't work.
            " name=\"{$rfilename}\"\n".
            "Content-Disposition: attachment;\n".
            " filename=\"{$rfilename}\"\n".
            "Content-Transfer-Encoding: base64\n\n".
            $resume."\n\n".
            $message."\n\n".
            "--{$mime_boundary}\n".
            "Content-Type: {$fileatttype};\n".
            " name=\"{$fileattname}\"\n".
            "Content-Disposition: attachment;\n".
            " filename=\"{$fileattname}\"\n".
            "Content-Transfer-Encoding: base64\n\n".
            $data."\n\n".
            "--{$mime_boundary}--\n";
        if(!mail($mailto,$subject,$message,$headers)){
            // mail failed!
            mail(
                $mailto,
                'ERROR in '.__FILE__,
                'Unable to send fdf file via attachment. Data follows:'."\n----- Begin -----\n$fdf\n----- End -----\n"
            );
Anyway... the mail and attachments work just fine. I only get "corrupt" data with Word 2007 documents. I have made sure that my server has registered the MIME types for Office 2007. I've also tried it without base64 encoding. I can't seem to fix it. Any ideas?

Thanks for reading this wall of text.