如何将Laravel 5.7“电子邮件验证"排队邮件发送 [英] How to queue Laravel 5.7 "email verification" email sending

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

问题描述

Laravel 5.7包含的电子邮件验证"功能效果很好,但不是异步电子邮件发送(在用户注册或重新发送链接页面期间)并不理想.

Laravel 5.7 included "email verification" feature works well but not async email sending (during user register or resend link page) is not ideal.

有什么方法可以通过队列发送电子邮件验证电子邮件,而无需在Laravel 5.7中重写整个电子邮件验证吗?

Is there any way to send the email verification email through a queue without rewrite whole email verification in Laravel 5.7 ?

推荐答案

是的!这是可能的.为此,您将必须在App\User中重写sendEmailVerificationNotification.此方法由Illuminate\Auth\MustVerfiyEmail特性提供.方法sendEmailVerificationNotification通过发送Illuminate\Auth\Notifications\VerifyEmail通知类中定义的电子邮件来通知创建的user.

Yes! It's possible. And to do that you will have to rewrite the sendEmailVerificationNotification in your App\User. This method is provide by the Illuminate\Auth\MustVerfiyEmail trait. The method sendEmailVerificationNotification notify the created user by sending an Email as define in the Illuminate\Auth\Notifications\VerifyEmail Notification class.

// This is the code define in the sendEmailVerificationNotification
public function sendEmailVerificationNotification()
{
    $this->notify(new Notifications\VerifyEmail);
}

您可以更改此方法以不直接通知用户.您将必须定义一个Job,您将在sendEmailVerificationNotification方法中使该路径变路径,而不是通知创建的用户.

You can change this method to not notify directly the user. You will have to define a Job which you will dispath in the sendEmailVerificationNotification method instead of notifying the created user.

在要在其中创建的Job类中,可以将电子邮件发送到user,但是必须将$user提供给Job,可以通过将其作为参数传递来执行该工作.像这样的dispatch方法

In the Job class which you will create in it handle method you can send the email to the user but you must provide the $user to the Job which can be perform by passing it as a parameter to the dispatch method like this

public function sendEmailVerificationNotification()
{
    VerifyEmail::dispatch($this);
}

方法中的

$this代表创建的user,您将创建的App\Jobs\VerififyEmail作业将接收传递给dispatch的所有参数,其中__construct

$this in the method represent the created user and the App\Jobs\VerififyEmail job which you will create will receive all the parameters pass to the dispatch in it __construct

VerifyEmail的代码如下所示

namespace App\Jobs;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Auth\Notifications\VerifyEmail;

class VerifyEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function handle()
    {
        // Here the email verification will be sent to the user
        $this->user->notify(new VerifyEmail);
    }
}

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

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