K
K
KevinSmash2019-04-25 11:20:44
Yii
KevinSmash, 2019-04-25 11:20:44

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();
    }

As a result, he tries to open a PDF file on the site, but an error occurs that he could not do this, and nothing comes to the mail, respectively.
$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]);

When using this code, the file came with pure html code. The question is what's wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-04-25
@KevinSmash

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

where is the real error code?
5. What's the point of changing the template if you then render the page bypassing the framework? As I understand it, instead of it should be either or depending on what the render method gives in the library known only to you , there are more details in the api to the framework: https://www.yiiframework.com/doc/api/2.0/yii-base-. .. https://www.yiiframework.com/doc/api/2.0/yii-base-...$this->layout = 'pdf';return $pdf->render();return $this->renderPartial($pdf->render());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question