Controller:
public function sendemail(Request $request)
{
$data = array(
'name'=> $request->name,
'email'=> $request->email,
'text'=> $request->text,
'category'=> $request->category,
'company'=> $request->company,
'number'=> $request->number
);
\Mail::send('AltHr/Portal/supportemail', $data, function ($message) use($data){
$message->from($data['email'], $data['name']);
$message->to('[email protected]')->subject($data['company'] . ' - ' .$data['category']);
$message->attach($request->file('files')->getRealPath(), [
'as' => $request->file('files')->getClientOriginalName(),
'mime' => $request->file('files')->getMimeType()
]);
});
return view('AltHr.Portal.support');
}
Blade:
<div class="form-group form-group-default">
<label>Attachment</label>
<input type="file" name="files[]" accept="file_extension|image/*|media_type" multiple>
</div>
I tried doing a simple contact form to send to my email. Currently it works im able to send that email but without attachments. So i tried doing the code for the attachments but it dont seem to be working im getting error:
Undefined variable: request
What am I doing wrong here?
Your $request
variable is undefined because $request
variable is not available inside the function you need to passed this below
\Mail::send('AltHr/Portal/supportemail', compact('data'), function ($message) use($data, $request){ .... });
Add the attr inside the form tag enctype="multipart/form-data"
<form role="form" action="{{action('AltHr\Portal\PortalController@sendemail')}}" method="post" class="m-t-15" enctype="multipart/form-data">
....
</form>
Try this code inside your sendemail() method
$data = array(
'name'=> $request->name,
'email'=> $request->email,
'text'=> $request->text,
'category'=> $request->category,
'company'=> $request->company,
'number'=> $request->number
);
$files = $request->file('files');
\Mail::send('AltHr/Portal/supportemail', compact('data'), function ($message) use($data, $files){
$message->from($data['email']);
$message->to('[email protected]')->subject($data['company'] . ' - ' .$data['category']);
if(count($files) > 0) {
foreach($files as $file) {
$message->attach($file->getRealPath(), array(
'as' => $file->getClientOriginalName(), // If you want you can chnage original name to custom name
'mime' => $file->getMimeType())
);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With