Friday, March 31, 2017

Laravel - Email Attachment Undefined variable

I have three files a user can upload but the issue is if an image isn't attached it throws an Undefined variable error, because nothing is being passed through, the main issue is that attachments are defined via the url, so when I add a null value or a '' to it, it throws a timeout error.

is there a better way/approach to tackling this?

if ($request->file('img_1')) {
            $file = $request->file('img_1');
            $image_name1 = time()."-".$file->getClientOriginalName();

            if ($file->move('uploads/quote', $image_name1)) {
                $enquiry->img_1 = $image_name1;
                $enquiry->save();
            }
        }else{
            $image_name1 = '';
        }

Here's the function I used after this

function ($message) use ($request, $image_name1, $image_name2, $image_name3) {

                $name_input = $request['name'];
                $company_name = 'Test';

                $message->to($sendto_email, '')->from('alerts@testing.com', 'Quote')->subject('New Quote from ' . $name_input )->attach('uploads/quote/' . $image_name1)->attach('uploads/quote/' . $image_name2)->attach('uploads/quote/' . $image_name3);
        });



via Harry

Advertisement