5

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']    = 'support@aurorax.co';
    $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('support@aurorax.co', 'aurora exchange');
    $this->email->to('masnad@aurorax.co');
    $this->email->subject('pdf');
    $this->email->attach($pdf);
    $this->email->message('Hello!');  

    $this->email->send();
}
user3378165
  • 6,546
  • 17
  • 62
  • 101
Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40

2 Answers2

3

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"
    }
EmcLIFT
  • 276
  • 3
  • 6
1

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.

Asif Rahaman
  • 775
  • 6
  • 10
  • Yea but I dont want it to store the email in some path, either I would store it in the database or send it as a email, so its a MUST for me not to store it in the drive – Masnad Nihit Jun 16 '16 at 08:59
  • @MasnadNihit which version of codeigniter are you using?? – Asif Rahaman Jun 16 '16 at 09:05
  • 2.1.4 is the version – Masnad Nihit Jun 16 '16 at 09:12
  • i don't think there exist any method which accepts raw data...you have to extend email class and override existing method. hope this link help you.. http://stackoverflow.com/questions/2146591/php-send-email-with-pdf-attachment-without-creating-the-file – Asif Rahaman Jun 16 '16 at 09:24
  • But is there a way to save it to the database and then export it out ? – Masnad Nihit Jun 16 '16 at 09:32
  • @MasnadNihit did you visit the link i gave? clearly there is a way(see the accepted answer) but he did it using php mail functions not codeigniter email class. So what you have to do is tweak existing methods of email class. – Asif Rahaman Jun 17 '16 at 03:36
  • Yup I did take a look but couldnt tweak it, could you help me out anyhow ? – Masnad Nihit Jun 17 '16 at 07:58