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);
$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!';
$mail->MsgHTML("This is a test");
$mail->AddAttachment($emailAttachment);
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
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.
source
share