dongtuo5262 2016-06-17 08:26
浏览 87

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

I am using dompdf to generate a pdf and I am taking the raw string, encoding it for saving it to the database later and then decoding it so I can send it as a attachment for the email, but I cannot do it anyhow. I tried both the examples here PHP Send email with PDF attachment without creating the file? and PHP get pdf file from base64 encoded data string but I could not get it working.

So here is the code

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']    = "
";

            $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();      

    }

Struggling for a 2 days now, cant get it working.

  • 写回答

1条回答 默认 最新

  • duanfu1942 2017-02-27 09:48
    关注

    I changed two lines of Codeigniter Email class, so that I can send base64 string as attachments easily.

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

    Download and replace your email class with this.

    Initialize config array as:

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

    Call the attachmnet function with mime type:

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么