how to random password generate and send to email in laravel all version?

The code write in below



  public function forgetPassword(Request $request)

    {

         $data = $request->all();

        $usecheck=User::where('email',$data['email'])->first();

        if(!empty($usecheck))

        {

     $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

          $pin = mt_rand(1000, 9999)

                . mt_rand(1000, 9999)

                . $characters[rand(0, strlen($characters) - 1)];

            // shuffle the result

            $string = str_shuffle($pin);

             try{

                           

                             Mail::send(['html' => 'mail'],

                    ['status' => 5,'email'=>$data['email'],'password' => $string], function ($message) use ($data) {

                        $message->to($data['email'])->subject

                            ('Congratulations, Your New Password Created Successfully');

                        $message->from('admin@gmail.com');

                    });


                         }

                    catch(Exception $exception)

                       {

                         return false;

                       }


                  User::where('email',$data['email'])->update(['password'=>bcrypt($string)]);

                   return back()

                        ->with('success','Your New Password Send Successfully');    

        }

        else

        {

            return back()

                        ->with('status','Your Email Id do not match Please try again.');

        }

    }


First of all create the function forgetPassword() with parametrize 

Call the route and above function pass the emailid

Check the email id exist in your database like this User::where('email',$data['email'])->first();

User is model create in the project 

If email id exist in your database after that randon generate password like this:

    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

          $pin = mt_rand(1000, 9999)

                . mt_rand(1000, 9999)

                . $characters[rand(0, strlen($characters) - 1)];

            // shuffle the result

            $string = str_shuffle($pin);

The generate number is $string to send the email id as write the code in to  above

In laravel one way password encrypt is like this : bcrypt($string) 

After that update the encrypted code in your database user table .

if you want to redirect the page and show the successfully  massage like this

return back() ->with('success','Your New Password Send Successfully');  

this fuction return same page and show the massage.



how to random password generate and send to email in laravel all version? how to random password generate and send to email in laravel all version? Reviewed by Bugs Solutions on December 13, 2020 Rating: 5

4 comments