Laravel如何将生成的PDF附加到电子邮件

时间:2019-08-13 12:55:07

标签: php laravel dompdf laravel-mail

我正在开发一个NEW应用程序,用户可以在其中填写并提交带有一些用户数据的表单。用户提交表单后,我会收到一封电子邮件,确认用户已向我发送了一些数据。 现在,除此以外,我还添加了一个功能,即用户数据也将生成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

那么,如何合并这两种邮件方法,使我只收到其中一种?

更新

确定,所以我尝试在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; } -其他建议?

2 个答案:

答案 0 :(得分:0)

按照documentation来附加文档(无论是pdf还是其他格式)的Laravel方法是Mail类的构建方法

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

答案 1 :(得分:0)

尝试以下示例

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

}