如何排队Laravel 5.7“电子邮件验证”电子邮件发送

时间:2018-10-04 10:52:40

标签: laravel queue email-verification laravel-5.7

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

是否可以通过队列发送电子邮件验证电子邮件,而无需在Laravel 5.7中重写整个电子邮件验证?

5 个答案:

答案 0 :(得分:7)

是的!这是可能的。为此,您将不得不在Azure Functions Core Tools (2.0.3) Function Runtime Version: 2.0.12115.0 中重写sendEmailVerificationNotification。此方法由App\User特性提供。方法Illuminate\Auth\MustVerfiyEmail通过发送sendEmailVerificationNotification Notification类中定义的电子邮件来通知创建的user

Illuminate\Auth\Notifications\VerifyEmail

您可以更改此方法以不直接通知用户。您将必须定义一个// This is the code define in the sendEmailVerificationNotification public function sendEmailVerificationNotification() { $this->notify(new Notifications\VerifyEmail); } ,您将在Job方法中使该路径变路,而不是通知创建的用户。

在您将在sendEmailVerificationNotification方法中创建的Job类中,您可以将电子邮件发送到handle,但必须将user提供给Job,通过将其作为参数传递给$user这样的方法

dispatch
方法中的

public function sendEmailVerificationNotification() { VerifyEmail::dispatch($this); } 代表创建的$this,您将创建的user作业将在其中App\Jobs\VerififyEmail接收传递给dispatch的所有参数}

__construct的代码如下:

VerifyEmail

答案 1 :(得分:7)

没有内置的方法,但是您可以通过扩展和覆盖来轻松实现。

首先,创建一个扩展内置通知的新通知,并实现ShouldQueue合同(以启用排队)。下列类假设您在app/Notifications/VerifyEmailQueued.php处创建了一个通知:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\VerifyEmail;

class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
    use Queueable;

    // Nothing else needs to go here unless you want to customize
    // the notification in any way.
}

现在,您需要告诉框架使用自定义通知而不是默认通知。您可以通过在sendEmailVerificationNotification()模型上覆盖User来做到这一点。这只是更改了发送哪些通知。

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

答案 2 :(得分:0)

感谢爱国者提供的代码像个魅力,但我不知道为什么要这么做:

class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
    public $connection;
    public $queue;
    public $delay;
    //
}

在我的案例中,必须公开这些变量。

谢谢!

答案 3 :(得分:0)

我的解决方案是,如果您要在控制器中手动注册用户。 Laravel已经创建了Registered事件及其侦听器SendEmailVerificationNotification。

-首先在应用程序中配置队列 .env文件更新QUEUE_CONNECTION=database中的内容。 有关更多队列文档的信息,请阅读https://laravel.com/docs/6.x/queues

  • 通过php artisan queue:table

  • 发布队列表
  • php artisan migrate

  • php artisan make:job EmailVerificationJob

  • 在EmailVerificationJob.php中添加公共变量

    公共$ user;

  • 在EmailVerificationJob.php构造函数中

    公共函数__construct(User $ user) {     $ this-> user = $ user; }

  • EmailVerificationJob.php中的
  • 处理函数写入event(new Registered($this->user))

  • 在控制器中(如果用户创建成功)添加此代码以使作业正常工作。

    EmailVerificationJob :: dispatch($ user)             ->延迟(now()-> addSeconds(5)); 这里的工作延迟了5秒。

  • 最后,您必须启动队列工作器php artisan queue:work --tries=3。这里尝试表示队列应尝试该工作多少次。

答案 4 :(得分:-2)

解决方案非常简单:

Steps:

  1. 配置队列驱动程序

  2. 转到->照亮\ Auth \ Notifications \ VerifyEmail

  3. 实现“ ShouldQueue”界面,并在上述类(即“ VerifyEmail”)上添加一个特征“ Queueable”:

VerifyEmail类扩展了Notification实施 ShouldQueue { 使用可排队;

... .... ... }

3。就这样

界面和特征的路径: 使用Illuminate \ Contracts \ Queue \ ShouldQueue; 使用Illuminate \ Bus \ Queueable;

也请检查文档: https://laravel.com/docs/5.7/notifications#queueing-notifications