简体   繁体   English

当用户在 Laravel 中更新电子邮件地址时发送电子邮件激活(确认)链接

[英]Sending email activation(confirmation) link when user UPDATING email address in Laravel

How can I resend my activation account email to my user once the user update the email address.用户更新电子邮件地址后,如何将我的激活帐户电子邮件重新发送给我的用户。

Email address should not be changed in the db until user click on the confirmation link在用户点击确认链接之前,不应在数据库中更改电子邮件地址

following is my update function for an user in my usercontroller(only the function body included)以下是我的用户控制器中用户的更新函数(仅包含函数体)

if($request->hasFile('propic'))
           {
                $this->validate($request, [
                    'name' => ['required', 'alpha','min:2', 'max:255'],
                    'last_name' => ['required', 'alpha','min:5', 'max:255'],
                    'mobile' => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|3[70]|7|1)\d{1,14}$/'],
                    'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
                    'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
                ],$request->all());

                $imageName = time().'.'.$request->propic->extension();  
                $request->propic->move(public_path('propics'), $imageName);
                $setting->propic=$imageName;
                $setting->name=$request->input('name');
                $setting->last_name=$request->input('last_name');
                $setting->mobile=$request->input('mobile');
                $setting->email=$request->input('email');
                $setting->update();
                return Redirect::back()->with('success',__('sentence.User updated successfully'));  
           }

and following is my update user form以下是我的更新用户表单

<form action="{{ route('settings.update',$user->id) }}" method="POST" enctype="multipart/form-data">
            <div class="row mt-5">
                <div class="col-sm-3">
                @if($user->propic != 'user-pic.png')
    <button type="submit" name="resetphoto" class="btn btn-warning  pull-right">{{ __('sentence.Remove Profile Pic') }}</button>
@endif
                    <img src="/propics/{{$user->propic}}" alt="Profile Pic" id="profile_pic_display" class="mb-3">
                    <input type="file" name="propic" class="form-control">

                    @error('propic')
                    <span class="help-block" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                    @enderror
                </div>
                <div class="col-sm-9">
                <!-- <form action="{{ route('settings.update',$user->id) }}" method="POST"> -->
                    @csrf
                    @method('PUT')
                    <div class="row">
                        <div class="col-md-6">

                            <div class="form-group field-user-firstname required">
                                <label class="control-label"
                                       for="user-firstname">{{ __('sentence.First Name') }}</label>
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror"
                                       name="name" value="{{$user->name}}" autocomplete="name" autofocus>

                                @error('name')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror

                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group field-user-lastname required">
                                <label class="control-label" for="user-lastname">{{ __('sentence.Last Name') }}</label>
                                <input id="last_name" type="text"
                                       class="form-control @error('name') is-invalid @enderror" name="last_name"
                                       value="{{$user->last_name}}" autocomplete="last_name" autofocus>

                                @error('name')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group field-user-mobile required">
                                <label class="control-label" for="user-mobile">{{ __('sentence.Mobile') }}</label><br/>
                                <input id="mobile_1" type="tel"
                                       class="form-control @error('mobile') is-invalid @enderror" name="mobile"
                                       value="{{$user->mobile}}" style="min-width:398px;" autocomplete="mobile"
                                       autofocus>

                                @error('mobile')
                                <span class="help-block" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group field-user-email required">
                                <label class="control-label" for="user-email">{{ __('sentence.Email') }}</label>
                                <input id="email_" type="email"
                                       class="form-control @error('email') is-invalid @enderror" name="email"
                                       value="{{$user->email}}" autocomplete="email" >

                                @error('email')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                    </div>
                    <div class="row">
                        <div class="col-sm">
                            <div class="form-group pull-right">
                                <button type="submit"
                                        class="btn btn-default">{{ __('sentence.Cancel') }}</button>
                                <button type="submit"
                                        class="btn btn-default subscribe px-5">{{ __('sentence.Update') }}</button>
                            </div>
                        </div>
                    </div>
                </div>

        </form>

Currently i'm sending activation link to user email when the user is registering, but I'm struggling to do it when an existing user is trying to update email address..目前,我正在用户注册时向用户电子邮件发送激活链接,但是当现有用户尝试更新电子邮件地址时,我很难做到这一点。

Your question isn't entirely clear because you're working in a box here and haven't given us the full picture.您的问题并不完全清楚,因为您在这里的一个盒子里工作并且没有给我们完整的图片。 If the intention is to ensure that the email cannot unless they've activated , then re-send them the activation email and confirm the new address upon activation, then you're missing some logic here.如果目的是确保除非他们已激活否则电子邮件无法发送,然后重新向他们发送激活电子邮件并在激活时确认新地址,那么您在这里缺少一些逻辑。

Any way that you look at it, you need to store a reference to what the email should be somewhere.无论您如何看待它,您都需要在某处存储对电子邮件内容的引用。 Either a column, such as pending_email on the table, or it's own table, such as pending_changes for instance.要么是一列,例如表上的pending_email ,要么是它自己的表,例如pending_changes

In your boot method of your User model you can watch for the updating event and dispatch a job from there to re-send the activation email and halt the change of the email address:在您的User模型的boot方法中,您可以监视updating事件并从那里分派作业以重新发送激活电子邮件并停止更改电子邮件地址:

public static function boot() 
{
    parent::boot();

    static::updating(function ($model) {
        if ($model->isDirty('email') && $model->activated_at === null) {
            $model->email = $model->getOriginal('email');
            dispatch (new SendActivationEmail($model));
        }
    });
}

Now you can append to that updating event to see if they're activating and have a pending_email :现在您可以附加到该updating事件以查看它们是否正在激活并有一个pending_email

static::updating(function ($model) {
    if ($model->isDirty('activated_at') && $model->pending_email) {
        $model->email = $model->pending_email;
        $model->pending_email = null;
    }
});

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

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