简体   繁体   中英

Mail not sending in laravel 5 from an ajax post request

I am working on forgot password functionality in my laravel 5.0 application. I am asking user to enter registered email and submit(post method) this form works on jquery ajax post request. After getting email from request i am checking it in db whether it exist or not. if exist i am updating new password, if not sending an error message upto this every this is fine. Now i am trying to send email with new password to the user, it is not working. Password is updating but email not sending.Find my controller code below

public function forgot_pass(Request $request){

        $email = $request->input('femail');
        $data = ['email' => $email];

        $user = Myuser::where($data)->count();

        if($user == 0){
            return response()->json(['status' => 'Invalid']);
        }
        else{

            $user_details = Myuser::where($data)->get()->first();

            $new_pass = $this->generateRandomString();

            $md5_pass = md5("EEE".$new_pass);

            $status = Myuser::where('email', $email)
                ->update(['pass' => $md5_pass]);

                if($status){

                    $mail_data = ['name' => $user_details->name, 'new_pass' => $new_pass];

                    Mail::send('emails.newpassword', $mail_data, function($message)
                    {
                        $message->from('us@example.com', 'Laravel');

                        $message->to($email);

                    });

                    return response()->json(['status' => 'Invalid']);
                }

        }

    }

Actually mail is working if i used $message->to('sometest@testmail.com'); instead of $message->to($email); . Below is the error i am getting.
在此处输入图片说明

Feeling strange why $email causing error.

You have to pass the $email to the anonymous function

Mail::send('emails.newpassword', $mail_data, function($message) use ($email) {
...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM