Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to queue Laravel 5.7 "email verification" email sending

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

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

like image 991
François Avatar asked Oct 04 '18 10:10

François


1 Answers

There is no built in way, but you can do it easily by extending and overriding.

First, create a new notification that extends the built-in notification, and also implements the ShouldQueue contract (to enable queuing). The following class assumes you create a notification at 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.
}

Now you need to tell the framework to use your custom notification instead of the default one. You do this by overriding the sendEmailVerificationNotification() on your User model. This simply changes which notification gets sent out.

public function sendEmailVerificationNotification()
{
    $this->notify(new \App\Notifications\VerifyEmailQueued);
}
like image 64
patricus Avatar answered Sep 29 '22 17:09

patricus