Laravel Event, Listeners, Email & Queue

Mosharrf Hossain
Mh Mohon
Published in
2 min readApr 12, 2019

Send Email Through Event, Listeners and queue

Step: 1— Creating Event & Listeners

Event can create two ways one is using Composer by running the command below:

php artisan make:event UserVerifyEvent

Another is by editing app/providers/EventServiceProvider.php

📝 Edit: Event Service Provider File

protected $listen = [ 'App\Events\UserVerifyEvent' => [    'App\Listerners\UserVerifyListener', ],];

Then run this command:

php artisan event:generate

Step: 2— Make Event & Listeners working & Create mail function

📝 Edit: UserVerifyEvent Event File

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

📝 Create: SendVerification Mailable File

We may create this mail file by this following command:

php artisan make:mail SendVerification

📝 Edit: UserVerifyListener Listener File

use App\Mail\SendVerification;
use App\Events\UserVerifyEvent;
use Mail;
class UserRegisterListener{ public function handle(UserVerifyEvent $event) { $email = $event->user->email; Mail::to($email)->send(new SendVerification($event->user)); }}

📝 Edit: SendVerification Mail File

class SendVerification extends Mailable{    public $user;    public function __construct($user)    {        $this->user = $user;     }    public function build()
{
return $this->view('emails.user.verify');
}
}

Step: 3— Trigger the event

We can fire the event in two ways one is from the model and one is manually.

Eloquent models fire several events, allowing you to hook into the following points in a model’s lifecycle: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored

To get started, define a $dispatchesEvents property on your Eloquent model that maps various points of the Eloquent model's lifecycle to your own event classes:

<?php

use App\Events\UserVerifyEvent;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;


protected $dispatchesEvents = [
'created' => UserVerifyEvent::class,

];
}

This will automatically trigger UserVerifyEvent.

Another way is:

event(new UserVerifyEvent($user));

Step: 4— Setup Queue/job

For Shared hosting or run locally we have to use database queue driver. we have to edit .env file

QUEUE_DRIVER=database

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

php artisan queue:table

php artisan migrate

This will create job table on database

Then we have to use implements ShouldQueue in our Listener file

use App\Mail\SendVerification;
use App\Events\UserVerifyEvent;
use Mail;
class UserRegisterListener implements ShouldQueue{ public function handle(UserVerifyEvent $event) { $email = $event->user->email; //Send Mail with a mailable class
Mail
::to($email)->send(new SendVerification($event->user));
}}

Then run this command

php artisan queue:work

WOW, ALL DONE, NOW LETS RUN OUR APPLICATION AND TEST OUT APPLICATION

Note — Some error with solution

  1. If the Queue not been processed weird infinite loop then it usually occurred if error happen in queue/job. see laravel.log or failed-job table if using driver database. it will show the error.
  2. If Queue generate previous request just delete data from job table and run these following code:
php artisan config:cache
php artisan queue:restart
php artisan queue:work

--

--