Laravel Event, Listeners, Email & Queue

Mosharrf Hossain
Apr 12, 2019 · 2 min read

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:


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


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
::to($email)->send(new SendVerification($event->user));

Then run this command

php artisan queue:work


Note — Some error with solution

php artisan config:cache
php artisan queue:restart
php artisan queue:work

Mh Mohon

This is My blog

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store