【问题标题】:Send pdf in Email Without Saving it on Server Using Codeigniter使用 Codeigniter 在电子邮件中发送 pdf 而不将其保存在服务器上
【发布时间】:2023-03-04 14:15:01
【问题描述】:

在我看来,我一直在尝试发送带有pdf 附件的电子邮件,该附件由MPDF 库在codeigniter 中生成,我已成功将我的文件转换为PDF,或者我认为我做到了,但我找不到任何方法使用 CodeIgniter 中的给定功能将该文件附加到电子邮件。

在 CodeIgnitor 的文档中,attach 函数的一个令人困惑且不完整的描述暗示了这一点:

$this->email->attach($buffer, '附件', 'report.pdf', '应用程序/pdf');

$buffer 是字符串,report.pdf 是电子邮件附件的文件名,

现在我关心的是我可以附加我生成的视图,如下所示:

ini_set('memory_limit', '20M');
// load library
$this->load->library('pdf');
$pdf = $this->pdf->load();

// boost the memory limit if it's low ;)
$html = $this->load->view('reports/someReport', array('var' => 123), true);
// render the view into HTML
$pdf->WriteHTML($html);
$this->email->attach($pdf->Output('', 'S'), 'attachment', 'report.pdf', 'application/pdf'); // is this okay or something is wrong here ?

我怎样才能成功地用电子邮件附加它并发送它

【问题讨论】:

    标签: php codeigniter email pdf


    【解决方案1】:

    首先,您可以将 PDF 文件保存在您的项目文件夹中,例如 attach。然后这个 pdf 位置设置电子邮件附件。像下面这样: :

    <?php
    ini_set('memory_limit', '20M');
    // load library
    $this->load->library('pdf');
    $pdf = $this->pdf->load();
    
    // boost the memory limit if it's low ;)
    $html = $this->load->view('reports/someReport', array('var' => 123), true);
    // render the view into HTML
    
    $pdfFilePath = FCPATH . "attach/pdf_name.pdf";
    $pdf->WriteHTML($html);
    $pdf->Output($pdfFilePath, "F");
    
    $result = $this->email
                ->from('example@test.com', 'From name')
                ->to('test@test.com')
                ->subject($subject)
                ->message($body)
                ->attach($pdfFilePath)
                ->send();
    $this->email->clear($pdfFilePath);
    if($result) {
        echo "Send";
        unlink($pdfFilePath); //for delete generated pdf file. 
    }
    ?>
    

    【讨论】:

    • 上述代码的问题是每次发送电子邮件时都会保存文件,有没有办法绕过它?
    • 告诉我。现在用 pdf 发送附件?
    • 让我测试一下。
    • 我收到了这个错误&lt;B&gt;mPDF error: &lt;/B&gt;Unable to create output file: /home/myusername/public_html/app_rest/attach/pdf_name.pdf ,这里app_rest文件夹是codeignitor的根目录
    • app_rest/attach文件夹中成功创建pdf文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2018-12-10
    相关资源
    最近更新 更多