John
John

Reputation: 565

Can't open pdf files created with mpdf in mail

I am trying to create a pdf document using mpdf and in the first step I tried to save it in my downloads folder and then I tried to open it and it worked perfect. I can see what I need in the pdf document.

In the next step I tried to send the pdf as an attachment and the email was sent successfully with the pdf attachment but when I tried to open it from the mail its returning me an error something as "Adobe cannot open the pdf document because it is neither a supported file type or the file is damaged.......".

I searched for different questions in stackoverflow and I tried their valuable answers too but none of them helped me.

The below is the same question but the answer posted there too didn't helped me.

Stackoverflow questions related to my post

Few Links Which I followed

Here's my code:

 $mpdf=new mPDF( ); 
 $mpdf->SetDisplayMode('fullpage');
 $mpdf->list_indent_first_level = 0;    
 $mpdf->WriteHTML($html,2);

 ob_clean(); // **Tried this from Stackoverflow answers**

 $mpdf->Output($filename.'.pdf', 'I'); 

 $mailto   = $row['Email1'].",".$row['Email5'];
 $path  =  'C:\\Users\\Downloads\\';
 $file  = $path."/".$filename;

 $subject   = 'Test Email';
 $message   = 'Test <br> Case';

 $content = file_get_contents($file);
 $content = chunk_split(base64_encode($content));

 $separator = md5(time());

 $eol = PHP_EOL;

 $headers = "From: John <[email protected]>".$eol;
 $headers .= "MIME-Version: 1.0".$eol;
 $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"".$eol.$eol;         

 // message
 $body .= "Content-Transfer-Encoding: 7bit".$eol;
 $body .= "This is a MIME encoded message.".$eol;
 $body = "--" . $separator.$eol;
 $body .= "Content-Type: text/html; charset=\"UTF-8\"".$eol;
 $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
 $body .= $message.$eol;

 // attachment
 $body .= "--" . $separator.$eol;
 $body .= "Content-Type: application/octet-stream; name=\"" . $filename . '.pdf' . "\"".$eol;
 $body .= "Content-Transfer-Encoding: base64".$eol;
 $body .= "Content-Disposition: attachment".$eol.$eol ;
 $body .= $content.$eol;
 $body .= "--" . $separator . "--";

 //SEND Mail
 if (mail($mailto, $subject, $body, $headers)) {
 //echo "mail send ... OK"; // or use booleans here
 } 

Upvotes: 2

Views: 2113

Answers (1)

Brainfeeder
Brainfeeder

Reputation: 2632

Have you tried changing the mime-type you set for the attachment? The standard mime-type for pdf files is:

application/pdf

I noticed you are trying to load a file from local file system. Does the script run on the same machine? Or on a webserver?

  • If it runs on a webserver you might need to upload the file to the webserver first. You'll need to change the $path and $file variables once uploaded.

    $path  =  'www/images/[some-path]';
    $file  = '/'.$filename;
    
  • If it runs on the same machine as where the file is located, you might need to update the $file variable so it uses a backslash \ instead of the forward slash.

    $path  =  'C:\\Users\\Downloads\\';
    $file  = $path.'\\'.$filename;
    

Another error in your script: You never set the mpdf contents as the content for the email attachment. Try something like:

$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'I')));

You'll have to check if the I in the Output() function of mpdf is correct for this use. I'm not sure of that.

EDIT

// First save it to the server somewhere 
$mpdf->Output($filename.'.pdf', 'F'); // Make sure the path is valid and writing permissions are set correctly to prevent writing errors.

// Then get the contents from the PDF
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'S')));

Upvotes: 1

Related Questions