php - 使用 Codeigniter 在电子邮件中发送 pdf 而不将其保存在服务器上

标签 php codeigniter email pdf

我一直在尝试发送带有由 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/

相关文章:

php - 如何在 PHP 中展平这个对象数组?

php - PHP PDO 语句可以接受表名或列名作为参数吗?

php - `static` 函数内部的关键字?

java - 未在使用 JavaMail api 发送的电子邮件中设置发件人

iphone - 将邮件从 postfix 推送到 iPhone

php - 关于将 Highstocks 图表图像发送到服务器的建议

javascript - jquery ajax 表单验证不适用于 GET 方法 codeigniter

codeigniter - 将多维数组从 Controller 传递到 View codeigniter

css - Markdown 邮件按钮 Laravel 无法正常工作

php - 使用 codeigniter 注册时遇到问题