How to create Event for Mail sending in Laravel ?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog we will see how to create event for mail sending in laravel application. Events provides us a simple observer and implementation, allowing us to subscribe and also listen for events in our application. So now in this posts we will learn how to create event for email send in our laravel application. We know event is very help to create a proper programming way. At First need to create event by using bellow command.

php artisan make:event SendMail

Ok, then we can see file in this path

app/Events/SendMail.php and now need put bellow code in that file.

app/Events/SendMail.php

namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class SendMail extends Event
{
    use SerializesModels;
    public $userId;
    public function __construct($userId)
    {
        $this->userId = $userId;
    }
    public function broadcastOn()
    {
        return [];
    }
}

Next, we have to create an event listener for “SendMail” event. So need to create event listener by using bellow command.

php artisan make:listener SendMailFired --event="SendMail"

So in this event listener we need to handle event code, i mean code of mail sending, also Before this file we need to check our mail configuration, If you did not set then we can set this way :How to set gmail configration for mail in Laravel?.

Then we need to put bellow code on app/Listeners/SendMailFired.php.

app/Listeners/SendMailFired.php

namespace App\Listeners;
use App\Events\SendMail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\User;
use Mail;
class SendMailFired
{
    public function __construct()
    {
        
    }
    public function handle(SendMail $event)
    {
        $user = User::find($event->userId)->toArray();
        Mail::send('emails.mailEvent', $user, function($message) use ($user) {
            $message->to($user['email']);
            $message->subject('Event Testing');
        });
    }
}

Now we need to register event on EventServiceProvider.php file so, open the  app/Providers/EventServiceProvider.php and then need copy this code and put in our file.

app/Providers/EventServiceProvider.php

namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        'App\Events\SomeEvent' => [
            'App\Listeners\EventListener',
        ],
        'App\Events\SendMail' => [
            'App\Listeners\SendMailFired',
        ],
    ];
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);
    }
}

Good now at Last we are ready to use event in our controller file. so need to use on this way:

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use Event;
use App\Events\SendMail;
class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
    {
        Event::fire(new SendMail(2));
        return view('home');
    }
}

Read Also : How to Create Custom Blade Directive in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →