Yii2 发送 pdf 到邮件不起作用?

Posted

技术标签:

【中文标题】Yii2 发送 pdf 到邮件不起作用?【英文标题】:Yii2 send pdf to mail not working? 【发布时间】:2018-03-28 14:14:21 【问题描述】:

我正在使用 kartik mpdf 扩展来生成使用下面提到的代码,它可以工作并在下一个选项卡中显示 pdf

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

现在要在邮件上发送生成的 pdf,我想在使用以下代码将邮件保存到服务器之前发送邮件

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

尽我所能点击api并生成pdf,但这也不起作用。

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

它也会返回 Null

【问题讨论】:

@Muhammad Omer Aslam 我一到家就会回复并会调查。 【参考方案1】:

首先我将生成的pdf保存在服务器目录中,然后使用以下代码发送成功后将其发送到邮件并取消链接

   $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');
                  

【讨论】:

@Muhammad Omer Aslam 我自己做的,先生,谢谢您的热情回复【参考方案2】:

我找不到确切的错误在哪里,但我已经通过 mpdf 成功生成了 pdf

  $model= $this->findModel($id);
    // get your HTML raw content without any layouts or scripts
    $content = $this->renderPartial('print_salaryslip',['model'=>$model]);

    // setup kartik\mpdf\Pdf component
    $pdf = new Pdf([
        // set to use core fonts only
        'mode' => Pdf::MODE_BLANK,
        // A4 paper format
        'format' => Pdf::FORMAT_A4,
        // portrait orientation
        'orientation' => Pdf::ORIENT_PORTRAIT,
        // stream to browser inline
        'destination' => Pdf::DEST_BROWSER,
        // your html content input
        'content' => $content,
        // format content from your own css file if needed or use the
        // enhanced bootstrap css built by Krajee for mPDF formatting
        'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
        // any css to be embedded if required
        'cssInline' => '.kv-heading-1font-size:18px',
        // set mPDF properties on the fly
        'options' => ['title' => 'Krajee Report Title'],
        // call mPDF methods on the fly
        'methods' => [
            'SetHeader'=>[' PAYSLIP'],
            'SetFooter'=>['PAGENO'],
        ]
    ]);

    // return the pdf output as per the destination setting
    return $pdf->render();

并确保$content是纯html。

【讨论】:

感谢您的回复。我还成功生成了 pdf,我遇到的问题是将生成的 pdf 发送到电子邮件。我也使用上面的代码成功发送了 pdf,但是 pdf 在我的邮件中没有正确打开。它返回错误 Yii::$app->mail->compose() ->attachContent($pathToPdfFile, [ 'fileName' => '你的 pdf 名称', 'contentType' => 'application/pdf ' ])->发送(); 我需要 $pathToPdfFile..? @Mercy peka 您始终可以将 pdf 文件保存在项目目录中并获取该 pdf 的路径。欢迎顺便说一句。【参考方案3】:

做这样的事情 ...

if (!empty($id)) 

            $emailSend = Yii::$app->mailer->compose(['html' =>'My-template-html'],[
                    'email' => $receiver->email,
                    'name'  => $receiver->name

                ])

                ->setFrom(["info@mail.com"])
                ->setTo($email)
                ->setSubject($subject)
                ->attach(Yii::getAlias('@backend').'/web/uploads/pdf/'.'Certificate'.'.pdf');
                return $emailSend->send();

               Yii::$app->session->setFlash('success', 'Email Sent Successfully');


             
...

【讨论】:

以上是关于Yii2 发送 pdf 到邮件不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

使用 Python Win32 发送电子邮件。将图像添加到电子邮件正文不起作用

使用 html Mailto 向多个收件人发送电子邮件:不起作用

自动雅虎邮件登录时,密码编辑框上的发送密钥命令不起作用

升级到 Yii2,ORM 不起作用

.Send 不起作用时如何发送邮件?

gmail api 发送邮件不起作用 C# 控制台应用程序(身份验证范围不足)