【问题标题】:Laravel How to attach generated PDF to EmailLaravel 如何将生成的 PDF 附加到电子邮件
【发布时间】:2019-08-13 12:55:07
【问题描述】:

我正在开发一个laravel-应用程序,用户可以在其中填写并提交包含一些用户数据的表单。当用户提交表单时,我会收到一封电子邮件,确认用户已向我发送了包含一些数据的表单。 现在,除此之外,我还添加了使用用户数据生成pdf 的功能,我想将其附加到电子邮件中。到目前为止它工作正常,但我的问题是现在我收到两封电子邮件。一份带有确认信息,一份带有生成的 PDF。

这是我的控制器的样子:

$application = Applicant::create([
    'email' => request()->email,
    'name' => request()->name,
    'avatar' => request()->avatar,
    'phone' => request()->phone,
    'address' => request()->address,
    'zipcode' => request()->zipcode,
    'city' => request()->city,
]);

$pdf = PDF::loadView('pdf.application', $application);

Mail::send('pdf.application', $pdf_data, function ($message) use ($pdf_data, $pdf) {
$message->to('my@mail.com', $application["name"])
    ->subject('New Applicant - ' . $application["name"])
    ->attachData($pdf->output(), "application_" . $application["name"] . ".pdf");
});

Mail::to('my@mail.com')->send(new NewApplication($application->fresh()));

return response()->json('OK', 200););

NewApplication-methods 构建函数如下所示:

public function build()
{
    $build = $this->replyTo($this->application->email, $this->application->name)
        ->subject("New Application in Database: {$this->application->name}")
        ->view('emails.application')
        ->with([
            'name' => $this->application->name,
            'address' => $this->application->address,
            'email' => $this->application->email,
            'phone' => $this->application->phone
        ]);
}

那么,我怎样才能将这两种邮件方式结合起来,让我只收到一个呢?

更新

好的,所以我尝试在我的 NewApplication.php 中执行此操作

public function __construct(SingleApplication $application)
{
    $this->application = $application;
    $this->pdf = PDF::loadView('pdf.application', $this->application);
}

public function build()
{
    $build = $this->replyTo($this->application->email, $this->application->name)
        ->subject("New application in database: {$this->application->name}")
        ->view('emails.application')
        ->attach($this->pdf, [
            'as' => 'applicant.pdf', 
            'mime' => 'application/pdf',
    ]);

    return $build;
}

但这失败并给了我错误basename() expects parameter 1 to be string, object given - 还有其他建议吗?

【问题讨论】:

    标签: php laravel dompdf laravel-mail


    【解决方案1】:

    按照documentation 附加文档(无论是 pdf 还是其他任何内容)的 Laravel 方法是在 Mail 类的 build 方法中

    ->attach($pdf->output(), [
          'as' => "application_" . $application["name"] . ".pdf",
          'mime' => 'application/pdf',
      ]);
    

    【讨论】:

    • 好的,但是如何包含专门为 pdf 创建的刀片视图?
    • 只需在 Mail 类的构造函数中接受它,就像您对 $application Eloquent 对象所做的那样
    • 嗯,这不起作用,因为 NewApplication-方法位于 Mail-文件夹中的不同文件中...我收到 call to undefined method output() 错误
    【解决方案2】:

    试试这个例子

        $this->validate($request, [
                'name' => 'required',
                'phone' => 'required',
                'email' => 'required|email',
                'message' => 'required|min:20',
                'checkbox' => 'required']);
    
        $data = array(
                'name' => $request->name,
                'phone' => $request->phone,
                'email' => $request ->email,
                'checkbox' => $request ->checkbox,
                'bodyMessage' => $request->message
            );
    
         //code to send email to my inbox
        Mail::send('emails.contact', $data, function($message) use ($data){
            $message->from($data['email']);
            $message->to('info@************');
        });
    
        //Feedback mail to client
        $pdf = PDF::loadView('your_view_name', $data)->setPaper('a4'); 
        Mail::send('emails.feedback', $data, function($message) use ($data,$pdf){
                $message->from('info@**********');
                $message->to($data['email']);
                $message->subject('Thank you message');
                //Attach PDF doc
                $message->attachData($pdf->output(),'customer.pdf');
            });
    
        Session::flash('success', 'Hello  '.$data['name'].' Thank You for choosing us. Will reply to your query as soon as possible');
    
        return redirect()->back();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 2012-05-30
      • 2017-08-17
      相关资源
      最近更新 更多