【问题标题】:Generate PDF from view to send email attaching the PDF file without saving it into disk Laravel 5从视图生成 PDF 以发送电子邮件附加 PDF 文件而不将其保存到磁盘 Laravel 5
【发布时间】:2017-11-17 12:41:24
【问题描述】:

我正在开发发送电子邮件功能。首先,我想从我的角度生成 .pdf 文件。然后我想通过电子邮件附加生成的 .pdf 文件而不将其保存到磁盘中。我在控制器中使用以下内容:

$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf");

我收到此错误:“在 null 上调用成员函数 attachData()”

如果我在下面不带附件使用,效果很好:

$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name));

请指教。

【问题讨论】:

    标签: laravel email attachment


    【解决方案1】:

    我认为您需要将其附加到邮件中,而不是邮件中。

    $pdf = PDF::loadView('getpdf', $data);
    $message = new Mysendmail($post_title, $full_name);
    $message->attachData($pdf->output(), "newfilename.pdf");
    Mail::to($to_email)->send($message);
    

    【讨论】:

    • 效果很好!我还有一个问题——我想对文件上传(.pdf / .doc)做同样的事情怎么样?我的意思是我不想将它保存到磁盘中,而是直接附加它。
    • 我认为这几乎是一样的。 attachData($file, '文件名')
    • 我试过这个。 attachData($request->file('file')->getClientOriginalExtension(), 'filename') 。结果,它确实发送了附件,但附件无法打开——它已损坏。
    • getClientOriginalExtension 给出文件的扩展名。试试 attachData($request->file('file')->getRealPath(), 'filename');
    • 我的工作方式是 $message->attach($request->file('file')->getRealPath(), array('as' => 'filename', 'mime' => $request->file('file')->getMimeType()));
    【解决方案2】:

    只需使用'mime' => 'application/pdf',最后你的代码。简单!

    ` $pdf = PDF::loadView('getpdf', $data); Mail::to($to_email)->send(new Mysendmail($post_title, $full_name)) ->attachData($pdf->output(), "newfilename.pdf"), [ 'mime' => '应用程序/pdf', ]); `

    【讨论】:

      【解决方案3】:

      只需使用'mime' => 'application/pdf',最后你的代码。简单!

      $pdf = PDF::loadView('getpdf', $data); 
      Mail::to($to_email)->send(new Mysendmail($post_title, $full_name)) 
      ->attachData($pdf->output(), "newfilename.pdf"), [ 
      'mime' => 'application/pdf', 
      ]);
      

      【讨论】:

        猜你喜欢
        • 2017-11-01
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-30
        • 2013-08-04
        • 1970-01-01
        相关资源
        最近更新 更多