How to create Event for Mail sending in Laravel 5.2?
In this post we will give you information about How to create Event for Mail sending in Laravel 5.2?. Hear we will give you detail about How to create Event for Mail sending in Laravel 5.2?And how to use it also give you demo for it if it is necessary.
Events provides a simple observer implementation, allowing you to subscribe and listen for events in your application. In this posts you can learn how to create event for email send in your laravel 5.2 application. event is very help to create proper progmamming way. First create event using bellow command.
php artisan make:event SendMail
Ok, now you can see file in this path
app/Events/SendMail.php and put bellow code in that file.
app/Events/SendMail.php
namespace AppEvents;
use AppEventsEvent;
use IlluminateQueueSerializesModels;
use IlluminateContractsBroadcastingShouldBroadcast;
class SendMail extends Event
{
use SerializesModels;
public $userId;
public function __construct($userId)
{
$this->userId = $userId;
}
public function broadcastOn()
{
return [];
}
}
Next, we need to create event listener for “SendMail” event. So create event listener using bellow command.
php artisan make:listener SendMailFired --event="SendMail"
In this event listener we have to handle event code, i mean code of mail sending, Before this file you have to check your mail configration, If you did not set then you can set this way :How to set gmail configration for mail in Laravel?.
Then you have to put bellow code on app/Listeners/SendMailFired.php.
app/Listeners/SendMailFired.php
namespace AppListeners;
use AppEventsSendMail;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use AppUser;
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 require to register event on EventServiceProvider.php file so, open app/Providers/EventServiceProvider.php and copy this code and put in your file.
app/Providers/EventServiceProvider.php
namespace AppProviders;
use IlluminateContractsEventsDispatcher as DispatcherContract;
use IlluminateFoundationSupportProvidersEventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'AppEventsSomeEvent' => [
'AppListenersEventListener',
],
'AppEventsSendMail' => [
'AppListenersSendMailFired',
],
];
public function boot(DispatcherContract $events)
{
parent::boot($events);
}
}
At Last we are ready to use event in our controller file. so use this way:
app/Http/Controllers/HomeController.php
namespace AppHttpControllers;
use AppHttpRequests;
use IlluminateHttpRequest;
use Event;
use AppEventsSendMail;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
Event::fire(new SendMail(2));
return view('home');
}
}
Ok Now check in your application,
Happy Code……
Hope this code and post will helped you for implement How to create Event for Mail sending in Laravel 5.2?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs