Emailing pdf generated by dompdf with codeigniter Emailing pdf generated by dompdf with codeigniter codeigniter codeigniter

Emailing pdf generated by dompdf with codeigniter


Your code helped me implement sending pdf attachments without saving them on server. I wasn't calling the render method.

    //load dompdf library    $this->load->library('PHPPdf');    $objPHPPdf = new PHPPdf();    $objPHPPdf->loadHtml($attachment, 'UTF-8');    // (Optional) Setup the paper size and orientation    $objPHPPdf->setPaper('A4', 'portrait');    // Render the HTML as PDF    $objPHPPdf->render();    $pdf = $objPHPPdf->output();    $this->load->library('email');    $this->email->initialize($this->config->item("email_config"));    $from = $this->config->item("email_from");    $this->email->from($from["from"], $from["from_name"]);    $this->email->to($to);    $this->email->subject('Sample');    $this->email->message('Sample message');    $this->email->attach($pdf, 'application/pdf', "Pdf File " . date("m-d H-i-s") . ".pdf", false);    $r = $this->email->send();    if (!$r) {        // "Failed to send email:" . $this->email->print_debugger(array("header")));    } else {        // "Email sent"    }


To actually send an attachment you have to generate the pdf file not just render it in browser as pdf. After $dompdf->render(); line add $dompdf->stream("attachment.pdf"); and while attaching use the absolute path of this attachment file. you can always unlink(delete) the attachment after sending the email.