You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yii2使用Kartik MPDF生成PDF后无法发送邮件求助

解决Kartik MPDF生成PDF后邮件发送的问题

看起来你已经搞定了PDF生成和前端预览,但卡在了邮件发送环节。别担心,咱们一步步来解决这个问题:

首先你需要调整MPDF的配置,让它能生成可用于邮件附件的内容(要么保存为服务器文件,要么直接获取PDF二进制内容),再通过邮件组件把它作为附件发送。

方法1:生成PDF文件后作为附件发送

这种方式会先把PDF保存到服务器临时目录,发送邮件时附上该文件,完成后可选择删除临时文件避免占用空间。

修改你的PDF生成代码:

use kartik\mpdf\Pdf;
use Yii;

// 生成PDF配置
$pdf = new Pdf([
    'mode' => Pdf::MODE_UTF8,
    'filename' => 'Bill_of_lading_'.$exportDetail->booking_number.'_'.$customerDetail->customer_name.'_'.$customerDetail->company_name.'.pdf',
    'content' => $this->renderPartial('landing', [
        'model' => $this->findModel($id),
    ]),
    'options' => [
        'title' => 'Bill of Lading'
    ],
    // 关键:设置输出目标为文件
    'destination' => Pdf::DEST_FILE,
    // 指定保存路径(用Yii的runtime目录,避免权限问题)
    'savePath' => Yii::getAlias('@runtime/pdf/')
]);

// 确保保存目录存在
$saveDir = Yii::getAlias('@runtime/pdf/');
if (!file_exists($saveDir)) {
    mkdir($saveDir, 0777, true);
}

// 执行生成,获取保存的文件路径
$filePath = $pdf->render();

// 发送邮件
try {
    Yii::$app->mailer->compose()
        ->setTo('recipient@example.com')
        ->setFrom('sender@example.com')
        ->setSubject('Your Bill of Lading')
        ->setTextBody('Please find attached your bill of lading PDF.')
        // 添加PDF附件
        ->attach($filePath, [
            'fileName' => $pdf->filename,
            'contentType' => 'application/pdf'
        ])
        ->send();

    // 发送完成后删除临时文件
    unlink($filePath);
    echo "邮件发送成功!";
} catch (\Exception $e) {
    echo "邮件发送失败:" . $e->getMessage();
    // 失败时也删除临时文件
    if (file_exists($filePath)) {
        unlink($filePath);
    }
}

方法2:直接获取PDF二进制内容作为附件(不保存文件)

如果不想在服务器生成临时文件,可直接获取PDF的二进制字符串,作为邮件附件发送:

use kartik\mpdf\Pdf;
use Yii;

// 修改配置,输出目标为字符串
$pdf = new Pdf([
    'mode' => Pdf::MODE_UTF8,
    'filename' => 'Bill_of_lading_'.$exportDetail->booking_number.'_'.$customerDetail->customer_name.'_'.$customerDetail->company_name.'.pdf',
    'content' => $this->renderPartial('landing', [
        'model' => $this->findModel($id),
    ]),
    'options' => [
        'title' => 'Bill of Lading'
    ],
    // 关键:设置输出目标为字符串
    'destination' => Pdf::DEST_STRING,
]);

// 获取PDF二进制内容
$pdfContent = $pdf->render();

// 发送邮件
try {
    Yii::$app->mailer->compose()
        ->setTo('recipient@example.com')
        ->setFrom('sender@example.com')
        ->setSubject('Your Bill of Lading')
        ->setTextBody('Please find attached your bill of lading PDF.')
        // 直接用二进制内容添加附件
        ->attachContent($pdfContent, [
            'fileName' => $pdf->filename,
            'contentType' => 'application/pdf'
        ])
        ->send();
    echo "邮件发送成功!";
} catch (\Exception $e) {
    echo "邮件发送失败:" . $e->getMessage();
}

注意事项

  • 确保你的邮件组件(Yii::$app->mailer)已正确配置SMTP等参数,否则邮件无法正常发送。
  • 使用renderPartial时,确保生成的内容没有多余布局或无效HTML,避免PDF格式错乱。

内容的提问来源于stack exchange,提问作者rajwa766

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:34:21