Attach PDF on the fly using mPDF and PHPmailer

I am using mPDF to generate a PDF at the click of a button. I am looking for a way to add PDF to an application using PHPmailer. Here is what I tried:

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

require_once('/inc/user_controller.php');
require_once('/inc/PHPMailer/class.phpmailer.php');
$user = new user();
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

    $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
      $mail->Send();
      echo "Message Sent OK</p>\n";
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }

I get this error: "Could not access the file:", that is, a bunch of garbage that I assume is coming from base64_encode. Ideally, a PDF will be displayed, downloaded and sent via email, and will not save a copy on my server. I can live with the created temp file, but have not tried it yet.

I tested the email functionality and works without adding attachments, so I know that the problems are with mPDF and attach it correctly.

+5
source share
1 answer

mpdf S , .

addAttachment phpmailer , , .

phpmailer AddStringAttachment:

AddStringAttachment($string,$filename,$encoding,$type)

http://phpmailer.worxware.com/index.php?pg=tutorial#3.2

+7

All Articles