在Laravel 5.4中通过电子邮件将PDF文档作为附件发送

时间:2018-03-06 07:04:48

标签: php laravel email-attachments

我正在使用Laravel的网络应用程序。我已编程,当用户在表单中输入数据时,数据将发送到我的电子邮件地址,然后用户会收到反馈消息。我想在反馈消息中附上PDF文档。寻求帮助如何解析从控制器到PDF的用户详细信息,以便我可以使用它然后附加到反馈消息 ..

〜问候

控制器

public function postContact(Request $request){

        $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
        Mail::send('emails.feedback', $data, function($message) use ($data){
            $message->from('info@**********');
            $message->to($data['email']);
            $message->subject('Thank you message');
            //Attach PDF doc
            $message->attachData($data, 'pdf.customer']);
        });

        Session::flash('success', 'Hello  '.$data['name'].' Thank You for choosing us. Will reply to your query as soon as possible');

        return redirect()->back();

    }

1 个答案:

答案 0 :(得分:1)

我建议您使用此软件包创建一个pdf文件,以后可以将其附加到邮件中。 Laravel-PDF generator

设置完上述包后,使用下面的示例代码生成pdf

public function postContact(Request $ request){

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

}