简体   繁体   English

邮件未从Ajax发布请求以Laravel 5发送

[英]Mail not sending in laravel 5 from an ajax post request

I am working on forgot password functionality in my laravel 5.0 application. 我正在使用laravel 5.0应用程序中的忘记密码功能。 I am asking user to enter registered email and submit(post method) this form works on jquery ajax post request. 我要求用户输入注册的电子邮件并提交(post方法)此表单适用于jquery ajax发布请求。 After getting email from request i am checking it in db whether it exist or not. 从请求中获取电子邮件后,我正在db中检查它是否存在。 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'); 如果我使用$message->to('sometest@testmail.com');实际上邮件是有效的$message->to('sometest@testmail.com'); instead of $message->to($email); 而不是$message->to($email); . Below is the error i am getting. 以下是我遇到的错误。
在此处输入图片说明

Feeling strange why $email causing error. 感到奇怪,为什么$email导致错误。

You have to pass the $email to the anonymous function 您必须将$email传递给匿名函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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