Search code examples
phpcodeigniteremailsendgriddompdf

How do I attach a generated pdf by dompdf into mail using sendgrid mail api?


I'm using dompdf to genearate a pdf ( $output = $dompdf->output(); ) and I need to attach it to phpmailer and mail it....

And I'm using sendgrid as mail services...

function sendEMailwithAttachment($mail_type, $mail_variable = array(), $subject, $from, $mailto, $username, $fileName, $filePath) { 

    $to = new SendGrid\Email($username, $mailto);
    $content = new SendGrid\Content("text/html", $message);
    $file = $filePath;
    $file_encoded = base64_encode(file_get_contents($file));
    $attachment = new SendGrid\Attachment();
    $attachment->setContent($file_encoded);
    $attachment->setType("application/text");
    $attachment->setDisposition("attachment");
    $attachment->setFilename($fileName);

    $mail = new SendGrid\Mail($from, $subject, $to, $content);
    $mail->addAttachment($attachment);

}

How can I pass $output value in email

Is any way to pass $output as $filePath?


Solution

  • Save your PDF file in the disk:

    $output = $dompdf->output();
    file_put_contents('output.pdf', $output);
    $fileName = 'output.pdf';  // Pass this variable to sendEMailwithAttachment function
    

    Then pass the file path to the mail sender. After sending remove your pdf file from server.

    Source: how to save DOMPDF generated content to file?