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

标签 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 天,无法正常工作。

最佳答案

我更改了两行 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');.

关于php - 解码 pdf 字符串并通过 codeigniter 电子邮件功能发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876639/

相关文章:

php - "Time ago"来自 MySQL 时间戳

php - ext/session 并发问题示例?

php - 用户已经被占用?

javascript - 我可以使用 javascript 发送电子邮件吗

php - Gmail 忽略我的 HTML 电子邮件

php - 在 Symfony2 中使用 Doctrine 和静态 YAML 数据库

php - Codeigniter 使用 Join 或 where 来验证一行结果

javascript - AJAX 无法在 codeigniter 中加载同一 DIV 中的页面

php - 使用数组构建 CSV

php - Laravel Gmail : Expected response code 250 but got code "535",,消息为“535-5.7.8 用户名和密码未被接受。在 535 5.7.8 了解更多信息