Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emailing pdf generated by dompdf with codeigniter

Tags:

I have a PDF which I generated from a html view in Codeigniter and now I want to send it to my email, but the trouble I am having is that it shows that there is a string in the $pdf but its empty when sent in my email. Here is the whole Codeigniter function.

public function generatePDF()
{
    require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');

    $dompdf = new Dompdf();
    $msg = $this->load->view('credit_agreement_view', '', true);
    $html = mb_convert_encoding($msg, 'HTML-ENTITIES', 'UTF-8');
    $dompdf->load_html($html);
    $paper_orientation = 'Potrait';
    $dompdf->set_paper($paper_orientation);

    // Render the HTML as PDF
    $dompdf->render();
    $pdf = $dompdf->output();

    // sending the pdf to email
    $this->load->library('email');
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = '[email protected]';
    $config['smtp_pass']    = '#######';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'text'; // or html
    $config['validation'] = TRUE; // bool whether to validate email or not    
    $this->email->initialize($config);

    $this->email->from('[email protected]', 'aurora exchange');
    $this->email->to('[email protected]');
    $this->email->subject('pdf');
    $this->email->attach($pdf);
    $this->email->message('Hello!');  

    $this->email->send();
}
like image 660
Masnad Nihit Avatar asked Jun 16 '16 08:06

Masnad Nihit


1 Answers

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"
    }
like image 135
EmcLIFT Avatar answered Sep 28 '22 03:09

EmcLIFT