Tuesday, October 18, 2016

Laravel Event | How to send Email using Laravel Event?

laravel event listener


1. First Configure your Email Setting in you project. Do the necessary changes on .env file.
2. Run the following command first inside of your project using cmd.

php artisan make:event SendMail
3. This will generate a SendMail.php file inside App\Events directory. The file would be like this after writing the necessary codes :
<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class SendMail
{
    use InteractsWithSockets, SerializesModels;
    public $id;

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

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

4. Now run the following command for crating a listener for the event.

php artisan make:listener SendMailFire --event="SendMail"
5. This will generate a SendMailFire.php file inside App\Listeners directory. The file would be like this after writing the necessary codes :
<?php

namespace App\Listeners;

use App\Events\SendMail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
use Mail;

class SendMailFired
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  SendMail  $event
     * @return void
     */
    public function handle(SendMail $event)
    {
          $user = User::find($event->id)->toArray();
            
          Mail::send('send', $user, function($message) use ($user) {
            $message->to($user['email']);
            $message->subject('Event Testing');
        });
    }
}
6. In above code, you are seeing a 'send' inside of your mail function. This actually a blade page which user will see. For demo purpose I'm sending the below message (send.blade.php):

<html>
    <head>
       <title>EVENT TEST MAIL</title>     
    </head>
    <body>
        <h3> Hello Event Testing ..</h3>
    </body>
</html>

7. Now Let's create a controller. So the request will be called. And event will be fired. Let's store some user data with name, email, password etc. So when a User's data saved into database the Event been fired and the User will get mail.

(UserController.php)
 public function postRegistration(Request $request)
    {
        $user = new User();         
        $user->name = $request->Input(['name']);
        $user->email=$request->Input(['email']);
        $user->password=$request->Input(['password'])       
        $user->save();        
        Event::fire(new SendMail($user->id));               
     return Response::json($user);
    }


















Related Articles

0 comments:

Post a Comment