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 ?


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

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.

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 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

The code of the VerifyEmail will look like this

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

Queues - Laravel, Queues. Configuration; Basic Usage; Queueing Closures; Running The Queue Listener; Daemon Queue Worker; Push Queues; Failed Jobs  In order to use the database queue driver, you will need a database table to hold the jobs. To generate a migration that creates this table, run the queue:table Artisan command. Once the migration has been created, you may migrate your database using the migrate command: php artisan queue:table php artisan migrate.


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

Laravel (5.7) Queues, env file, and you are good to go. Also, we need to change the queue driver to the database. By default laravel uses sync. APP_NAME=Laravel  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 ?


Laravel Queues Tutorial, Setup Database for Laravel Queues. We will use Database drivers for our Laravel Queue example. You can use the following command to create  The queue configuration options are in the .env file. If you would like to thoroughly customize the queue configuration, then you must copy the entire vendor/laravel/lumen-framework/config/queue.php file to the config directory in the root of your project and adjust the necessary configuration options as needed.


The solution is pretty simple:

Steps:

  1. Configure Queue Driver

  2. Go To --> Illuminate\Auth\Notifications\VerifyEmail

  3. Implement 'ShouldQueue' interface and add a trait 'Queueable' on above mentioned class i.e. 'VerifyEmail' like this:

class VerifyEmail extends Notification implements ShouldQueue{ use Queueable;

.... .... ... }

3.That's it

Path of interface & trait: use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Bus\Queueable;

Please check the docs too: https://laravel.com/docs/5.7/notifications#queueing-notifications

A simple guide to properly queuing mail in Laravel, There aren't many useful guides out there that provide a simple, straight-forward answer how to use Laravel queues to send e-mails in batches. Laravel Queues Tutorial With Example From Scratch is today’s leading topic. Laravel 5.5 queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time-consuming task, such as sending an email.


How to queue Laravel 5.7 "email verification" email sending, Yes! It's possible. And to do that you will have to rewrite the sendEmailVerificationNotification in your App\User . This method is provided by the  Install Laravel 5.7 App. First of we need to download laravel 5.7 fresh setup. Use the below command and download fresh new laravel setup : composer create-project --prefer-dist laravel/laravel LaravelPdf Setup Database. After successfully download laravel 5.7 Application, Go to your project .env file and set up database credential and move next step : DB_CONNECTION=mysql


How to Queue Emails in Laravel PHP with Twilio SendGrid, Laravel 5.7+ comes with the email verification feature by default, so we will use the default functionality first. Later on we will switch to Laravel  in this laravel 5.7 tutorial we learn how to make a simple get api with example. we fetch data from database and a quick tutorial from get api example is here.


Why Laravel Queues Are Awesome ― Scotch.io, Queues in Laravel are used to make a smooth sailing application cycle by stacking heavy tasks to be handled as jobs and dispatching these  As explained in a previous dive, calls on the Queue facade are calls on the queue driver your app uses, calling the push method for example is a call to the push method of the Queue\DatabaseQueue class in case you're using the database queue driver.