Search code examples
phplaravelemail-attachments

Send a PDF document as an attachment via email in Laravel 5.4


I am working on a web app in Laravel. I have programmed such that when a user enters data in a form, the data is send to my email address, the user then gets a feedback message. I want to attach a PDF document to the feedback message. Seeking assistance on how to parse the user details from the controller to the PDF so that I can use it then attach to the feedback message..

~ Regards

Controller

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

    }

Solution

  • I would suggest you to use this package for creating a pdf file which can be attached later for sending it to mail. Laravel-PDF generator

    after settingup the above package use the below sample code for generating 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();
    
    }