解码pdf字符串并通过codeigniter电子邮件功能发送它

时间:2016-06-17 08:26:38

标签: php codeigniter email pdf dompdf

我正在使用dompdf来生成pdf,我正在使用原始字符串,将其编码以便稍后将其保存到数据库然后解码它以便我可以将其作为电子邮件的附件发送,但无论如何我无法做到。我在这里尝试了PHP Send email with PDF attachment without creating the file?PHP get pdf file from base64 encoded data string这两个例子,但我无法让它发挥作用。

所以这是代码

public function generateCreditAggreement(){
        $this->load->helper('file'); 
        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);

//            $dompdf->set_paper('A4', 'Potrait');
            $paper_orientation = 'Potrait';

            $dompdf->set_paper($paper_orientation);

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

            // Output the generated PDF to Browser
//          $dompdf->stream('document.pdf');
//          $id = "sup";
//          $dompdf->stream("$id.pdf", array("Attachment" => false));           

            $pdf_string =   $dompdf->output();

            $new_pdf = write_file('name', $pdf_string);


            $pdf_content =  base64_encode($pdf_string);
//          $this->load->model('agreement_page');
//          $pushString = $this->agreement_page->storePdfString($pdf_content);

            //Decode pdf content
            $pdf_decoded = base64_decode ($pdf_content);
            //Write data back to pdf file
            $pdf = fopen ('credit_agreement.pdf','w');
            fwrite ($pdf,$pdf_decoded);
            //close output file
            fclose ($pdf);




            // 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('myfile.pdf' , $pdf,'application/pdf');
            $this->email->message('Your pdf is here');  
//            $this->email->attach($pdf);
            $this->email->send();      

    }

现在苦苦挣扎了2天,无法让它发挥作用。

1 个答案:

答案 0 :(得分:1)

我更改了两行Codeigniter Email类,以便我可以轻松地将base64字符串作为附件发送。

https://github.com/aqueeel/CI-Base64EmailAttach

使用此功能下载并替换您的电子邮件类。

将配置数组初始化为:

$config['_encoding'] = 'base64';
$this->load->library('email',$config);

使用mime类型调用attachmnet函数:

$this->email- >attach($base64,'attachment','report.pdf','application/pdf');.