我一直在尝试发送带有由 codeigniter 中的 MPDF
库生成的 pdf
附件的电子邮件,从我看来,我已成功将文件转换为 PDF
或者我想我做到了,但我找不到任何方法使用 CodeIgniter 中的给定功能将该文件附加到电子邮件。
在 CodeIgnitor 文档中,对 attach
函数的困惑且不完整的描述表明:
$this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
其中$buffer
是字符串report.pdf
将是电子邮件附件的文件名,
现在我关心的是我可以附加我生成的 View ,如下所示:
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 ?
如何通过电子邮件成功附加并发送
最佳答案
首先,您可以将 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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="107568717d607c7550647563643e737f7d" rel="noreferrer noopener nofollow">[email protected]</a>', 'From name')
->to('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="314554424571455442451f525e5c" rel="noreferrer noopener nofollow">[email protected]</a>')
->subject($subject)
->message($body)
->attach($pdfFilePath)
->send();
$this->email->clear($pdfFilePath);
if($result) {
echo "Send";
unlink($pdfFilePath); //for delete generated pdf file.
}
?>
关于php - 使用 Codeigniter 在电子邮件中发送 pdf 而不将其保存在服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777597/