Answer the question
In order to leave comments, you need to log in
How to send an email as a PDF file?
Hello, I'm trying to implement the function of sending an invoice to the mail. In the view, next to each email there is a button to send a message. A separate send file has been created in which the invoice html code is written. And in the controller, respectively, public function actionSend($id)
{
$this->layout = 'pdf';
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$headers = Yii::$app->response->headers;
$headers->add('Content-Type', 'application/pdf');
$model = $this->findModel($id);
$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
'content' => $this->render('send', ['model'=>$model]),
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.img-circle {border-radius: 50%;}',
'options' => [
'title' => $model->title,
'subject' => 'PDF'
],
'methods' => [
'SetFooter' => ['|{PAGENO}|'],
]
]);
$content = $pdf->content;
$filename = $pdf->filename;
$sendemail=Yii::$app->mailer->compose()
->attachContent($content, [
'fileName' => $filename,
'contentType' => 'application/pdf'
])
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Проверка')
->send();
return $pdf->render();
}
$model = $this->findModel($id);
Yii::$app->mailer->compose()
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Проверка подписки')
->setTextBody($this->render('send', ['model' => $model]))
->send();
return $this->render('send', ['model' => $model]);
Answer the question
In order to leave comments, you need to log in
1. What library are you using? What's in new Pdf
?
2. check that in $content
3. look at the logs, the failure to send mail may have nothing to do with pdf
4.
but the error is that it failed
$this->layout = 'pdf';
return $pdf->render();
return $this->renderPartial($pdf->render());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question