doumeikuan6834 2018-03-28 14:14
浏览 84
已采纳

Yii2发送pdf到邮件不工作?

I am using kartik mpdf extension to generate using the mention below code it work and show the pdf in next tab

  $pdf = new Pdf([
              'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
              'filename' => 'Bill_of_lading_'.$exportDetail->booking_number.'_'.$customerDetail->customer_name.'_'.$customerDetail->company_name.'.pdf',
              'content' => $this->renderPartial('landing', [
                  'model' => $this->findModel($id),
              ]),
              'options' => [
                  'title' => 'Privacy Policy - Krajee.com',
                  'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
              ],
              'methods' => [
                  'SetHeader' => ['Generated By: ARIANA WORLDWIDE||Generated On: ' . date("r")],
                  'SetFooter' => ['|Page {PAGENO}|'],
              ]
          ]);
return $pdf->render();

Now for send the generatd pdf on mail i want to send the mail before save it on server using below code

$content = $pdf->content;
      $filename = $pdf->filename;
$sendemail=Yii::$app->mail->compose()
             ->attachContent($content, [
                           'fileName'    => $filename,
                            'contentType' => 'application/pdf'
                           ])
                             ->setFrom('mushahidh224@gmail.com')
                             ->setTo('rajwabarocho@gmail.com')
                             ->setSubject('Design Beta sending subject here')
                             ->send();

Try my best to hit api and generate pdf but this also doesnt work.

  $mpdf = $pdf->getApi();
       $mpdf->WriteHTML($content); 
       $path = $mpdf->Output(Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf', 'F');

It also retrn Null

  • 写回答

2条回答 默认 最新

  • douxiza9868 2018-03-29 07:07
    关注

    First i have save the generated pdf in the server directory and then send it to mail and unlink after sending succcessfully using the below code

       $pdf = new Pdf([
                  'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
                  'filename' => 'Bill_of_lading_'.$exportDetail->booking_number.'_'.$customerDetail->customer_name.'_'.$customerDetail->company_name.'.pdf',
                  'content' => $this->renderPartial('landing', [
                      'model' => $this->findModel($id),
                  ]),
                  'options' => [
                      'title' => 'Privacy Policy - Krajee.com',
                      'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
                  ],
                  'methods' => [
                      'SetHeader' => ['Generated By: ARIANA WORLDWIDE||Generated On: ' . date("r")],
                      'SetFooter' => ['|Page {PAGENO}|'],
                  ]
              ]);
             if($mail){
                 $content = $pdf->content;
                 $filename = $pdf->filename;
             //  $mpdf = $pdf->getApi();
          //  $mpdf->WriteHtml($content);
    
          $path = $pdf->Output($content,Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf',\Mpdf\Output\Destination::FILE);
    
                 $sendemail=Yii::$app->mail->compose()
                 ->attach(Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf')
                                 ->setFrom('mushahidh224@gmail.com')
                                 ->setTo('rajwabarocho@gmail.com')
                                 ->setSubject('Design Beta sending subject here')
                                 ->send();
                                 if($sendemail)
                                 {
            unlink(Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf');
                        return $this->render('mailed');
                      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?