0
votes

I was trying to send a password reset link to my newly created user and had a hard time trying to find a solution. In case someone has the same issue here is what worked for me in Laravel 7.

Or is there a better way to handle all: add entry in password_resets table and send the notification? Mail sending properly but i want to send password reset mail to user .Below is the my add controller

public function contactSave(Request $request)
    {

        $password = Hash::make($request->name);

        $validate = $request->validate([
                        'name' => 'required|regex:/^[\pL\s\-]+$/u',
                        'email' => 'email',
                        'phone' => 'digits:10',
                        'address' => 'required',
                        'country' => 'required',
                        'state' => 'required',
                        // 'comment' => 'required',
                        'organization' => 'required',
                        'captcha' => 'required|captcha'
                    ],
                    [
                'captcha.captcha' => 'Incorrect Captcha'
            ]
                );

        $user= new User;
        $user->name = $request->name;
        $user->email = $request->email;     
        $user->password = $password;
        $user->save();


        $CustomerContact= new CustomerContact;

        // $CustomerContact->name = $request->name;
        // $CustomerContact->email = $request->email;
        $CustomerContact->phone = $request->phone;
        $CustomerContact->address = $request->address;
        $CustomerContact->user_id = $user->id;
        $CustomerContact->country_id = $request->country;
        $CustomerContact->state_id = $request->state;
        $CustomerContact->comment = $request->comment;
        $CustomerContact->organization = $request->organization;
        $CustomerContact->captcha = $request->captcha;

        $CustomerContact->save();

        $details = [
            'title' => 'Hi '.$request->name.'',
            'body' => 'Your account has been created successfully. Request you set your password with this link ???'
        ];
       
        \Mail::to($request->email)->send(new \App\Mail\ContactMail($details));

        return redirect('contacts')->with('success', 'User created successfully.');

        
    }  

Anyone here to help me. After saving data in user and employee . I want send email with password reset link to that user id email($request->email). GIve me simplest solution with kittle explation how to do this

2
did you solve it? - sta
@sta , No still not getting any Solution. - Praful

2 Answers

0
votes

if i correctly understood your problem you can create a route for example: password/reset/user-code and send it in your email to every user. you should create a random-code for every user when its created and put it in that link and send in your email. when user click on that link it will come to your controller and you check that random-code in your database and find your user! and when you have your user you can show them your view for resetting password and save new password that what i did for password reset before

0
votes

IF you want to send some data (link or token or everything else) to your mail use this:

Mail::to($request->email)->send(new ContactMail($your_token,$link,$user));

and in your ContactMail class:

    class ContactMail extends Mailable
{
   
 use Queueable, SerializesModels;
    public $your_token;
    public $link;
    public $user;


public function __construct($your_token,$link,$user)
{
    $this->your_token= $your_token;
    $this->link= $link;
    $this->user= $user;
}


public function build()
{
    return $this->view('email')
        ->with('your_token',$this->your_token)
        ->with('link',$this->link)
        ->with('user',$this->user
}

}