user()->first(); $email = $user->email; $link = $request->input('u'); $link = filter_var($link, FILTER_VALIDATE_URL); if ($link === false) { return response()->json(['error' => 'Invalid link.'], 422); } $title = $request->input('t') ?: ''; $email_sent = mail($email, 'New link : '.$title, $link); if ($email_sent) { return response()->json(['info' => 'Email sent.']); } return response()->json(['error' => 'An error occured while sending email'], 500); } }