如何使用 Codeigniter 将 Mpdf 输出附加为电子邮件附件?

How to attach Mpdf output as email attachment with Codeigniter?

在我的控制器中生成 pdf 布局,然后将其附加为 Pdf 并使用自定义文件名。这是代码

$this->load->library('mpdf');
$mpdf = new Mpdf('L', 'A4', 0, 'Trebuchet MS', 6, 6, 9, 9, 3, 3, 'L');
ob_start();
echo $this->generateFoodReport($inspectionId);
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML(utf8_encode($html));
$content = chunk_split(base64_encode($mpdf->Output('', 'S')));
$filename = "inspection-report.pdf";

$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($fromMail, $fromName);
$this->email->to($mailTo);
$this->email->subject($subject);
$this->email->attach($content):
$this->email->send();

如何使用电子邮件附加文件。

将纯 PDF 缓冲区发送到邮件 class。

$content = $mpdf->Output('', 'S');
$this->email->attach($content, 'attachment', $filename, 'application/pdf');

Email Class Codeigniter documentation

试试下面的代码

$this->load->library('mpdf');
$mpdf = new Mpdf('L', 'A4', 0, 'Trebuchet MS', 6, 6, 9, 9, 3, 3, 'L');
ob_start();
echo $this->generateFoodReport($inspectionId);
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML(utf8_encode($html));
$content = chunk_split(base64_encode($mpdf->Output('', 'S')));
$filename = "inspection-report.pdf";

$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($fromMail, $fromName);
$this->email->to($mailTo);
$this->email->subject($subject);
$this->email->attach($content, 'attachment', $filename, 'application/pdf');
$this->email->send();

更多请查看Codeigniter Email Class documentation