In this sample is for sending email using queue.
-
First we need to create a basic skelton poject of laravel. I'm using laravel 5.7
laravel new my-laravel-queue -
There after we need the following table to maintain the queue
php artisan queue:tableafter the migration created migrate the migration to real tables using migrate command of artisian
php artisan migrate -
There after we need to adding a Route to sending Email we adding that route as /sendEmail in our main route. which is define in web.php file
Route::get('/sendEmail',function (){ return 'Email Sent'; }); -
There after create queue job to sending email using the following command.
php artisan make:job SendEmailJobthere is a file will be created on app/Jobs/SendEmailJob.php
In the above mentioned file adding the Mail sending command on handle() method
Mail::to('[email protected]')->send(new SendEmailMailable()); -
There after
dipatch()the Job in route. (You can dispatch the job in Controller too.)Route::get('/sendEmail',function (){ $job = (new \App\Jobs\SendEmailJob())->delay(now()->addSeconds(5)); dispatch($job); return 'Email Sent'; });
That's all about it. you can send your email and can check the email on mailtrap.io.
Don't forgot to setup your credintials on .env file of yours