Laravel 7 Sending Mail using Event Example from Scratch

In this tutorial i will show you how to send email uisng laravel event. Send mail using event in larave 7 is the todays tutorial topic. We can send email in laravel using many system.

But in this tutorial i am going to show you how we can send email in laravel 7 using laravel events. Do you know what is events in laravel ? i you don't know then please go laravel documentation and read about laravel events.

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. 

So let's our today's tutorial  how to send email using laravel 7 using laravel events. Let's start.

how-to-create-event-for-send-email-in-laravel

Now lets start following to complete Event for Mail sending in Laravel.

Step 1 : Create Event

Run bellow command to create Laravel Event.

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 : Gmail configuration setup to send mail in Laravel

Read alsoAjax jQuery Insert Data 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');
        });
    }
}

Registering Events

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

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

Hope it can help you.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website