How to create Event for Mail sending in Laravel 5.2? (ok)

https://www.itsolutionstuff.com/post/how-to-create-event-for-mail-sending-in-laravel-52example.html

C:\xampp\htdocs\reset\app\Providers\EventServiceProvider.php

<?php
namespace App\Providers;
use App\Events\LoginHistory;
use App\Listeners\storeUserLoginHistory;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider {
  /**
   * The event listener mappings for the application.
   *
   * @var array<class-string, array<int, class-string>>
   */
  protected $listen = [
    Registered::class   => [
      SendEmailVerificationNotification::class,
    ],
    LoginHistory::class => [
      StoreUserLoginHistory::class,
    ],
  ];
  /**
   * Register any events for your application.
   *
   * @return void
   */
  public function boot() {
    //
  }
}

Sử dụng lập trức nó tại ra 2 file tương ứng trong Events, Listeners

php artisan event:generate

C:\xampp\htdocs\reset\app\Events\LoginHistory.php

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class LoginHistory {
  use Dispatchable, InteractsWithSockets, SerializesModels;
  public $user;
  /**
   * Create a new event instance.
   *
   * @return void
   */
  public function __construct($user) {
    $this->user = $user;
  }
  /**
   * Get the channels the event should broadcast on.
   *
   * @return \Illuminate\Broadcasting\Channel|array
   */
  public function broadcastOn() {
    return new PrivateChannel('channel-name');
  }
}

C:\xampp\htdocs\reset\app\Listeners\storeUserLoginHistory.php

<?php
namespace App\Listeners;
use App\Events\LoginHistory;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class storeUserLoginHistory {
  /**
   * Create the event listener.
   *
   * @return void
   */
  public function __construct() {
    //
  }
  /**
   * Handle the event.
   *
   * @param  \App\Events\LoginHistory  $event
   * @return void
   */
  public function handle(LoginHistory $event) {
    $current_timestamp = Carbon::now()->toDateTimeString();
    $userinfo          = $event->user;
    $saveHistory       = DB::table('login_history')->insert(
      array(
        'name'       => $userinfo->name,
        'email'      => $userinfo->email,
        'created_at' => $current_timestamp,
        'updated_at' => $current_timestamp,
      )
    );
    return $saveHistory;
  }
}

C:\xampp\htdocs\reset\app\Http\Controllers\Auth\LoginController.php

<?php
namespace App\Http\Controllers\Auth;
use App\Events\LoginHistory;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller {
  /*
  |--------------------------------------------------------------------------
  | Login Controller
  |--------------------------------------------------------------------------
  |
  | This controller handles authenticating users for the application and
  | redirecting them to your home screen. The controller uses a trait
  | to conveniently provide its functionality to your applications.
  |
   */
  use AuthenticatesUsers;
  /**
   * Where to redirect users after login.
   *
   * @var string
   */
  protected $redirectTo = RouteServiceProvider::HOME;
  /**
   * Create a new controller instance.
   *
   * @return void
   */
  protected function authenticated() {
    $user = Auth::user();
    event(new LoginHistory($user));
  }
}

Laravel 8 Events and Listeners with Practical Example

#php#laravel#webdev#beginners

1Laravel 8 Events and Listeners with Practical Example2Schedule a task to run at a specific time in laravel (CronJob)

Hello, today I want to talk about Events and Listeners in Laravel, in programming to write a scalable, reusable and clean code, you need to follow some programming principles, one of which is SOLID, I will not be going deep into explaining that today, but I will just highlight one, the S which stands for Single Responsibility Principle, this principle states that

A class should have one and only one reason to change, meaning that a class should have only one job.

What this means is that, A class should only perform one task, many times, we always load our class with some many functionalities, so if the class changes, a lot of things will break in our application, which is not a good practice. So I will introduce Events and Listeners to help in making our class perform just one task.

Click on my profile to follow me and get more updates.

What is An Event?

Events are the ways we hook into the activities of our application, it is just a way to observe an activity, for instance, login, a class can be created to monitor the activity of login, when a user logs in, the event class can execute some functions.

What is A Listener?

A Listener is a class that listens to the events that they are mapped to and execute a task, that is they are the ones that perform a given task for an event. Let me illustrate, you might want to send a welcome email to a new user of your application, and also assign a role to the user based on the information provided in the registration, etc, you would not want to do all those in the RegisterController because that we violate the first principle of SOLID, where the Controller will perform more than one task, the RegisterController needs to only perform the activity of registration of a new user. so an event needs to be carried out in the process of registration, where assigning a role, sending an email, etc are the individual listeners under the event. For this article, I will write one listener in an event, what the listener will do is to store the login of each user of the app in a table, this is just an illustration that will show you how it works. If you have a laravel project that has auth already, you can follow immediately, or you can follow my article Basic Laravel Login and Registration using Laravel Breeze, I will be using the project on that article from my system.

Step 1: Register the event and listeners in the EventServiceProvider

<?php

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Events\LoginHistory;
use App\Listeners\storeUserLoginHistory;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        LoginHistory::class => [
            StoreUserLoginHistory::class,
        ]
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

We added another event class called LoginHistory and also a listener called StoreUserLoginHistory, and noticed up that we called the class here in this way use App\Events\LoginHistory; and use App\Listeners\storeUserLoginHistory;, don't worry, I know you are wondering that the class does not exist in our application, we are going to generate it in the next step, you can add as many events and listeners as possible like this and even more

 protected $listen = [
        Event1::class => [
            Listener1::class,
            Listener2::class
        ],
        Event2::class => [
            Listener5::class,
            Listener7::class
        ],
        Event3::class => [
            Listener4::class,
            Listener7::class,
            Listener9::class
        ],
 ];

Step 2: Generate Events & Listeners

Previously, we write classes of events and listeners in the EventServiceProvider, so to generate it at once, we run this command

php artisan event:generate

Step 3: Write the Events and Listener class

Remember what we are trying to achieve was to store all the login of our app in a table, so click on the app/Events/LoginHistory.php and edit as follows

class LoginHistory
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

from the code above, the event accepts the $user which is the information of the user, and it will pass it to the listener. Click on app/Listeners/storeUserLoginHistory.php, this is where we are going to be writing the main logic of the storing of the login history, inside the handle method, add the following code

    public function handle(LoginHistory $event)
    {
        $current_timestamp = Carbon::now()->toDateTimeString();

        $userinfo = $event->user;

        $saveHistory = DB::table('login_history')->insert(
            ['name' => $userinfo->name, 'email' => $userinfo->email, 'created_at' => $current_timestamp, 'updated_at' => $current_timestamp]
        );
        return $saveHistory;
    }

Also remember to call the Carbon and DB facade before the class

use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

Step 4: Create the Table and Migrate

Step 5: Dispatch the Event

This is the last step, we need to call the event in the LoginController, if you are using laravel 7, or below, you can create a method in the LoginController.php like this

    protected function authenticated() {

        $user = Auth::user();

        event(new LoginHistory($user));
    }

but for this article, I am using Laravel Breeze, a simple UI scaffolding that was released 16 days ago, so I will go to LoginRequest.php found in app/Http/Requests/Auth/LoginRequest.php and inside the authenticate() method, I will call my event and pass the $user to the class

  public function authenticate()
    {
        $this->ensureIsNotRateLimited();

        if (! Auth::attempt($this->only('email', 'password'), $this->filled('remember'))) {
            RateLimiter::hit($this->throttleKey());

            throw ValidationException::withMessages([
                'email' => __('auth.failed'),
            ]);
        }

        $user = Auth::user();

        event(new LoginHistory($user));

        RateLimiter::clear($this->throttleKey());
    }

Follow me for more of my articles, you can leave comments, suggestions, and reactions. I am open to any vacancy as a PHP backend engineer, my strength is in the Laravel framework

click the link to view my profile and follow me

How to create Event for Mail sending in Laravel 5.2?

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 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 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 App\Listeners;use App\Events\SendMail;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use App\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 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 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);    }}

At Last we are ready to use event in our controller file. so use this way:

app/Http/Controllers/HomeController.php

Read Also: Laravel Mailchimp api integration from scratch with example

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

Ok Now check in your application,

Happy Code......

Last updated

Was this helpful?