Yii2 - 即时生成 Pdf 并附加到电子邮件
Yii2 - Generate Pdf on the fly and attach to email
这就是我生成 pdf 页面的方法
public function actionPdf(){
Yii::$app->response->format = 'pdf';
$this->layout = '//print';
return $this->render('myview', []);
}
这就是我发送电子邮件的方式
$send = Yii::$app->mailer->compose('mytemplate',[])
->setFrom(Yii::$app->params['adminEmail'])
->setTo($email)
->setSubject($subject)
->send();
如何生成 pdf 文件并将其即时附加到我的电子邮件中?
Mailer 有一个名为 attachContent()
的方法,您可以在其中放置 pdf 文件。
PDF 应在输出目标设置为字符串的情况下呈现,然后将其作为参数传递给 attachContent()
。
样本:
Yii::$app->mail->compose()
->attachContent($pathToPdfFile, [
'fileName' => 'Name of your pdf',
'contentType' => 'application/pdf'
])
// to & subject & content of message
->send();
我就是这样做的
在我的控制器中:
$mpdf=new mPDF();
$mpdf->WriteHTML($this->renderPartial('pdf',['model' => $model])); //pdf is a name of view file responsible for this pdf document
$path = $mpdf->Output('', 'S');
Yii::$app->mail->compose()
->attachContent($path, ['fileName' => 'Invoice #'.$model->number.'.pdf', 'contentType' => 'application/pdf'])
这就是我在 Yii2
中发送邮件的方式
private function sendPdfAsEmail($mpdf)
{
$mpdf->Output('filename.pdf', 'F');
$send = Yii::$app->mailer->compose()
->setFrom('admin@test.com')
->setTo('to@gmail.com')
->setSubject('Test Message')
->setTextBody('Plain text content. YII2 Application')
->setHtmlBody('<b>HTML content.</b>')
->attach(Yii::getAlias('@webroot').'/filename.pdf')
->send();
if($send) {
echo "Send";
}
}
- 将
mpdf
实例传递给我们的自定义函数。
- 使用
mpdf
中的 F
选项将输出保存为文件。
- 在 Yii 邮件程序中使用
attach
选项并设置保存文件的路径。
这就是我生成 pdf 页面的方法
public function actionPdf(){
Yii::$app->response->format = 'pdf';
$this->layout = '//print';
return $this->render('myview', []);
}
这就是我发送电子邮件的方式
$send = Yii::$app->mailer->compose('mytemplate',[])
->setFrom(Yii::$app->params['adminEmail'])
->setTo($email)
->setSubject($subject)
->send();
如何生成 pdf 文件并将其即时附加到我的电子邮件中?
Mailer 有一个名为 attachContent()
的方法,您可以在其中放置 pdf 文件。
PDF 应在输出目标设置为字符串的情况下呈现,然后将其作为参数传递给 attachContent()
。
样本:
Yii::$app->mail->compose()
->attachContent($pathToPdfFile, [
'fileName' => 'Name of your pdf',
'contentType' => 'application/pdf'
])
// to & subject & content of message
->send();
我就是这样做的
在我的控制器中:
$mpdf=new mPDF();
$mpdf->WriteHTML($this->renderPartial('pdf',['model' => $model])); //pdf is a name of view file responsible for this pdf document
$path = $mpdf->Output('', 'S');
Yii::$app->mail->compose()
->attachContent($path, ['fileName' => 'Invoice #'.$model->number.'.pdf', 'contentType' => 'application/pdf'])
这就是我在 Yii2
private function sendPdfAsEmail($mpdf)
{
$mpdf->Output('filename.pdf', 'F');
$send = Yii::$app->mailer->compose()
->setFrom('admin@test.com')
->setTo('to@gmail.com')
->setSubject('Test Message')
->setTextBody('Plain text content. YII2 Application')
->setHtmlBody('<b>HTML content.</b>')
->attach(Yii::getAlias('@webroot').'/filename.pdf')
->send();
if($send) {
echo "Send";
}
}
- 将
mpdf
实例传递给我们的自定义函数。 - 使用
mpdf
中的F
选项将输出保存为文件。 - 在 Yii 邮件程序中使用
attach
选项并设置保存文件的路径。