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

查看:53
本文介绍了如何自定义来自 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 中添加电子邮件验证时,建议的方法是实现 IlluminateContractsAuthMustVerifyEmail 并在 AppUser 模型上使用 IlluminateAuthMustVerifyEmail trait.

When you want to add Email Verification in Laravel 5.7 the suggested method is to implement IlluminateContractsAuthMustVerifyEmail and use the IlluminateAuthMustVerifyEmail trait on the AppUser Model.

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

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 NotificationsMustVerifyEmail 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 AppNotificationsCustomVerifyEmail);
}

...然后在您的 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.

电子邮件验证通知流程

当新用户注册时,IlluminateAuthEventsRegistered 事件会在 AppHttpControllersAuthRegisterController 中发出,并且 Registered 事件有一个名为 IlluminateAuthListenersSendEmailVerificationNotification 的监听器,它在 AppProvidersEventServiceProvider 中注册:

When a new user signs-up an IlluminateAuthEventsRegistered Event is emitted in the AppHttpControllersAuthRegisterController and that Registered event has a listener called IlluminateAuthListenersSendEmailVerificationNotification which is registered in the AppProvidersEventServiceProvider:

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

SendEmailVerificationNotification 侦听器检查 $user 是否作为参数传递给 new Registered($user = $this->create($request->all())) 在 Laravel 默认认证 AppHttpControllersAuthRegisterController - 是 IlluminateContractsAuthMustVerifyEmail 的一个实例,它是名字当您想要提供默认电子邮件验证并检查 $user 尚未验证时,Laravel 建议在 AppUser 模型中使用的特征.如果所有这些都通过,则对该用户调用 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 AppHttpControllersAuthRegisterController – is an instance of IlluminateContractsAuthMustVerifyEmail which is the name of the trait that Laravel suggests is used in the AppUser 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天全站免登陆