如何自Laravel 5.7定制电子邮件验证电子邮件? [英] How to customize the email verification email from Laravel 5.7?

查看:87
本文介绍了如何自Laravel 5.7定制电子邮件验证电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到Laravel 5.7,现在我正在使用内置的电子邮件验证.但是,有两件事我无法弄清,主要问题是如何定制发送给用户的电子邮件以验证其电子邮件?如果用户更改了电子邮件,我也无法弄清楚如何启动发送该电子邮件,但是我可以将其保存在另一个线程中.

I just upgraded to Laravel 5.7 and now I am using the built in Email Verification. However there is 2 things I have not been able to figure out and the primary issue is how can I customize the email that is being sent to the user for verifying their email? I also can't figure out how to initiate sending that email if the users changes their email but I can save that for another thread.

推荐答案

当您想在 Laravel 5.7 中添加电子邮件验证时,建议的方法是实现Illuminate\Contracts\Auth\MustVerifyEmail并使用Illuminate\Auth\MustVerifyEmail特性在App\User模型上.

When you want to add Email Verification in Laravel 5.7 the suggested method is to implement Illuminate\Contracts\Auth\MustVerifyEmail and use the Illuminate\Auth\MustVerifyEmail trait on the App\User Model.

要进行一些自定义行为,您可以覆盖方法sendEmailVerificationNotification,该方法通过调用方法notify通知创建的用户,并作为参数传递Notifications\MustVerifyEmail类的新实例.

To make some custom behaviour you can override the method sendEmailVerificationNotification which is the method that notifies the created user by calling the method notify, and passes as a parameter a new instance of the Notifications\MustVerifyEmail class.

您可以创建一个自定义通知,该通知将作为参数传递给用户模型中sendEmailVerificationNotification方法中的$this->notify():

You can create a custom Notification which will be passed as a parameter to the $this->notify() within the sendEmailVerificationNotification method in your User Model:

public function sendEmailVerificationNotification()
{
    $this->notify(new App\Notifications\CustomVerifyEmail);
}

...然后在CustomVerifyEmail通知中,您可以定义处理验证的方式.您可以通过发送带有自定义 verification.route 的电子邮件来通知创建的用户,该电子邮件将包含您想要的任何参数.

...then in your CustomVerifyEmail Notification you can define the way the verification will be handled. You can notify created user by sending an email with a custom verification.route which will take any parameters that you want.

电子邮件验证通知流程

新用户注册时,会在App\Http\Controllers\Auth\RegisterController中发出Illuminate\Auth\Events\Registered事件,并且该Registered事件具有一个名为Illuminate\Auth\Listeners\SendEmailVerificationNotification的侦听器,该侦听器已注册在App\Providers\EventServiceProvider中:

When a new user signs-up an Illuminate\Auth\Events\Registered Event is emitted in the App\Http\Controllers\Auth\RegisterController and that Registered event has a listener called Illuminate\Auth\Listeners\SendEmailVerificationNotification which is registered in the App\Providers\EventServiceProvider:

protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ]
];

SendEmailVerificationNotification侦听器检查$ user(在Laravel默认身份验证App\Http\Controllers\Auth\RegisterController中作为参数传递给new Registered($user = $this->create($request->all()))的参数)是否是Illuminate\Contracts\Auth\MustVerifyEmail的实例,该实例是Laravel建议的特征名称当您要提供默认电子邮件验证并且还检查$user尚未验证时,请在App\User模型中使用.如果所有步骤均通过,则在该用户上调用sendEmailVerificationNotification方法:

The SendEmailVerificationNotification listener checks if the $user – which is passed as a parameter to new Registered($user = $this->create($request->all())) in the Laravel default authentication App\Http\Controllers\Auth\RegisterController – is an instance of Illuminate\Contracts\Auth\MustVerifyEmail which is the name of the trait that Laravel suggests is used in the App\User Model when you want to provide default email verification and also check that $user is not already verified. If all that passes, the sendEmailVerificationNotification method is called on that user:

if ($event->user instanceof MustVerifyEmail && !$event->user->hasVerifiedEmail())   {
    $event->user->sendEmailVerificationNotification();
}

这篇关于如何自Laravel 5.7定制电子邮件验证电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆