Take the 2-minute tour ×

I'm using mPDF to generate PDF's when a button is clicked. I am looking for a way to add the PDF to an attachment using PHPmailer. Here is what I've tried:

$mpdf = new mPDF(); 
$emailAttachment = $mpdf->Output('filename.pdf','S');
$emailAttachment = chunk_split(base64_encode($emailAttachment));    

$user = new user();

    $currentUserInfo = $user->userDetails($userId);

    try {
      $mail->AddAddress($currentUserInfo['emailAddress'], $currentUserInfo['firstName']);
      $mail->SetFrom('name@yourdomain.com', 'First Last');
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->Subject = 'Your file is attached';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML("This is a test");
      $mail->AddAttachment($emailAttachment);      // attachment
      echo "Message Sent OK</p>\n";
    } catch (phpmailerException $e) {
    } catch (Exception $e) {

I get this error "Could not access file:" then a bunch of junk, which I assume comes from the base64_encode. Ideally, the PDF would display or download and be emailed as well without saving a copy on my server. I can live with a temp file being created, but haven't tried that yet.

I've tested the email functionality and it works without adding an attachment, so I know the issues is with mPDF and attaching it properly.

share|improve this question
add comment

1 Answer

When you use mpdf's S option the file is returned as a string and the file name parameter is ignored.

However phpmailer's addAttachment function takes a file path as its first parameter not a file as a string, hense the error you are seeing.

You should take a look at an alternative phpmailer function called AddStringAttachment:



share|improve this answer
add comment

Your Answer


