Laravel 5 missmatch token in sending email

时间:2020-12-27 18:16:33

i'm using ajax to send email so i my problem is if i dont have

我正在使用ajax发送电子邮件,所以我的问题是,如果我没有

{!! csrf_field() !!}

{! csrf_field()!!}

in view i will get the token missmatch error.If i add input hidden token in view i will get this error

在视图中我将获得令牌失配错误。如果我在视图中添加输入隐藏令牌,我将收到此错误

Swift_RfcComplianceException in MailboxHeader.php line 348: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.

MailboxHeader.php第348行中的Swift_RfcComplianceException:给定[]的邮箱中的地址不符合RFC 2822,3.6.2。

How can i fix it.Thanks for help.

我该如何解决它。谢谢你的帮助。

here it's view

这是它的观点

 <form action="{{ url('/findpass')}}" method="POST" id="forgotpassForm">
                                    {!! csrf_field() !!}
                                    <div class="form-group">
                                       <input class="form-control input-lg" placeholder="E-mail" name="emailForgot" type="email" id="emailForgot" required>
                                    </div>
                                    <input type="submit" class="btn btn-lg btn-primary btn-block" id="forgotpassBtn" value="Send email">
                            </form>

Ajax

$(document).ready(function() {
    $('#forgotpassBtn').click(function() {
        event.preventDefault();
        var email=$("#emailForgot").val();
        var _token = $("input[name='_token']").val();
        $.ajax({
            url: '/findpass',
            type: 'POST',
            data:{
                email:email,
                _token : _token  
            },
            success:function(data) {
                alert(data.mess);
                $("#forgotpass")[0].reset();
            },
            error:function() {
                alert("Error");
            }
        });


    });
});

Controller

public function resetPass()
    {
        $emailForgot=Input::get('emailForgot');
        $user= User::where('email', '=' , $emailForgot)->first();
        $data=['username'=>$user -> username,'email'=>$user -> email,'token'=>$user -> remember_token];
        Mail::send('ui.mail.reset',$data, function ($m) use ($user) {
            $m->from('ngohungphuc95@gmail.com', 'Reset password');
            $m->to($user->email, $user->full_name)->subject('Email reset password');
        });
        return response()->json(array('mess'=>'Mail send'));
    }

3 个解决方案

#1


0  

Add the csrf without blade and add an id by yourself. Get the value of csrf value by id using jquery. Here is how you can do this.

添加没有刀片的csrf并自己添加id。使用jquery通过id获取csrf值的值。这是你如何做到这一点。

Change {!! csrf_field() !!} to:

改变{!! csrf_field()!!}来:

<input type="hidden" name="_token" value="{{ csrf_token() }}" id="token"/>

And Change this line of ajax code:

并更改此行的ajax代码:

var _token = $("input[name='_token']").val();

To:

var _token = $('#token').val();

May be this is not the best way. But I solved my problem with this procedure.

可能这不是最好的方法。但我用这个程序解决了我的问题。

#2


0  

You could also add a global csrf token to all ajax calls like this

您还可以向所有类似的ajax调用添加全局csrf标记

        $(document).ready(function(){
        $.ajaxSetup({
            headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') },
        });
    });

Then just override the tokensMatch() method of the VerifyCsrfToken token to also look for the ajax header

然后只需覆盖VerifyCsrfToken令牌的tokensMatch()方法,也可以查找ajax头

    protected function tokensMatch($request)
{
    // If request is an ajax request, then check to see if token matches token provider in
    // the header. This way, we can use CSRF protection in ajax requests also.
    $token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');

    return $request->session()->token() == $token;
}

After this all your ajax calls will automatically pass csrf checks and be secure.

在此之后,所有ajax调用将自动通过csrf检查并保证安全。

#3


0  

Swift_RfcComplianceException in MailboxHeader.php line 348: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.

MailboxHeader.php第348行中的Swift_RfcComplianceException:给定[]的邮箱中的地址不符合RFC 2822,3.6.2。

The above error is because you haven't configured a from address in config/mail.php. It is set to null by default, but should be a valid email address:

上述错误是因为您尚未在config / mail.php中配置发件人地址。默认设置为null,但应该是有效的电子邮件地址:

'from' => ['address' => 'user@example.com', 'name' => null],

#1


0  

Add the csrf without blade and add an id by yourself. Get the value of csrf value by id using jquery. Here is how you can do this.

添加没有刀片的csrf并自己添加id。使用jquery通过id获取csrf值的值。这是你如何做到这一点。

Change {!! csrf_field() !!} to:

改变{!! csrf_field()!!}来:

<input type="hidden" name="_token" value="{{ csrf_token() }}" id="token"/>

And Change this line of ajax code:

并更改此行的ajax代码:

var _token = $("input[name='_token']").val();

To:

var _token = $('#token').val();

May be this is not the best way. But I solved my problem with this procedure.

可能这不是最好的方法。但我用这个程序解决了我的问题。

#2


0  

You could also add a global csrf token to all ajax calls like this

您还可以向所有类似的ajax调用添加全局csrf标记

        $(document).ready(function(){
        $.ajaxSetup({
            headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') },
        });
    });

Then just override the tokensMatch() method of the VerifyCsrfToken token to also look for the ajax header

然后只需覆盖VerifyCsrfToken令牌的tokensMatch()方法,也可以查找ajax头

    protected function tokensMatch($request)
{
    // If request is an ajax request, then check to see if token matches token provider in
    // the header. This way, we can use CSRF protection in ajax requests also.
    $token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');

    return $request->session()->token() == $token;
}

After this all your ajax calls will automatically pass csrf checks and be secure.

在此之后,所有ajax调用将自动通过csrf检查并保证安全。

#3


0  

Swift_RfcComplianceException in MailboxHeader.php line 348: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.

MailboxHeader.php第348行中的Swift_RfcComplianceException:给定[]的邮箱中的地址不符合RFC 2822,3.6.2。

The above error is because you haven't configured a from address in config/mail.php. It is set to null by default, but should be a valid email address:

上述错误是因为您尚未在config / mail.php中配置发件人地址。默认设置为null,但应该是有效的电子邮件地址:

'from' => ['address' => 'user@example.com', 'name' => null],