in this blog you are going to see that whenever you want to send email to multiple user then your view page takes a lot of time. so how can you decrease the load time and send email in background.
first of all you should create a folder Jobs under app and add two files eg. job.php and MailJob.php and now see both files code and add it to your files as well.
create a file to this location (app/Jobs/job.php) and put below codes in it.
<?php | |
namespace App\Jobs; | |
use Illuminate\Bus\Queueable; | |
abstract class Job | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Queueable Jobs | |
|-------------------------------------------------------------------------- | |
| | |
| This job base class provides a central location to place any logic that | |
| is shared across all of your jobs. The trait included with the class | |
| provides access to the "onQueue" and "delay" queue helper methods. | |
| | |
*/ | |
use Queueable; | |
} |
Now create another file name MailJob.php and put below code:
<?php | |
namespace App\Jobs; | |
use App\Jobs\Job; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Support\Facades\Log; | |
use Illuminate\Support\Facades\Mail; | |
use App\Mail\SampleEmailClassFile; | |
use Illuminate\Http\Request; | |
class IntervalTaskCreationMailJob extends Job implements ShouldQueue | |
{ | |
use InteractsWithQueue, SerializesModels; | |
protected $emailJobParams; | |
/** | |
* Create a new job instance. | |
* | |
* @return void | |
*/ | |
public function __construct($emailJobParams) | |
{ | |
$this->emailJobParams = $emailJobParams; | |
} | |
/** | |
* Execute the job. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
Log::info('Sample Mail Job START! ...'); | |
$this->sendIntervalTaskCreationMail($this->emailJobParams); | |
Log::info('Sample Mail Job END! ...'); | |
} | |
private function sendSampleMail($emailParams) | |
{ | |
Mail::to($emailParams->to_mail)->send(new SampleMail($emailParams)); | |
} | |
} |
and add below code to your SampleMail.php (app/Mail) location
<?php | |
namespace App\Mail; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Mail\Mailable; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
class IntervalTaskCreationMail extends Mailable | |
{ | |
use Queueable, SerializesModels; | |
private $emailParams; | |
/** | |
* Create a new message instance. | |
* | |
* @return void | |
*/ | |
public function __construct($emailParams) | |
{ | |
$this->emailParams = $emailParams; | |
} | |
/** | |
* Build the message. | |
* | |
* @return $this | |
*/ | |
public function build() | |
{ | |
return $this->from('sample@gmail.com', 'Sample Name') | |
->subject($this->emailParams->subject) | |
->view('mails.sampleemailcreationmail') | |
->cc('test@gmail.com') | |
->with( | |
[ | |
'emailParams' => $this->emailParams | |
]); | |
} | |
} |
now change some code in your store function of your Controller.php
<?php | |
namespace App\Http\Controllers\Admin; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use Brian2694\Toastr\Facades\Toastr; | |
use Illuminate\Support\Facades\Log; | |
use Mail; | |
use Validator; | |
use App\Jobs\IntervalTaskCreationMailJob; | |
class RegularTaskController extends Controller | |
{ | |
public function index() | |
{ | |
// | |
} | |
public function create() | |
{ | |
// | |
} | |
public function store(Request $request) | |
{ | |
$this->validate($request,[ | |
'users' => 'required', | |
'task_freq' => 'required', | |
'name' => 'required|string', | |
'email' => 'required|string|email|max:255', | |
]); | |
$task = new RegularTask(); | |
$task->task_freq = $request->task_freq; | |
$task->save(); | |
$task->users()->attach($request->users); | |
$allemails = $request->users; | |
$userArr = User::whereIn('id', $allemails)->get(); | |
Log::info('$emailParams :'); | |
$emailParams = new \stdClass(); | |
Log::info('$emailParams->subject :'); | |
$emailParams->subject = 'New Sample Task Created'; | |
Log::info($emailParams->subject); | |
$emailParams->senderEmail = $request->email; | |
$emailParams->TaskFrequency = $request->task_freq; | |
$emailParams->creationDate = $task->created_at; | |
$emailParams->receiverName = 'Admin'; | |
Log::info("$emailParams->receiverName:"); | |
foreach($userArr as $user){ | |
$emailParams->to_mail = $user->email; | |
$this->dispatch(new MailJob($emailParams)); | |
} | |
Log::info("$emailParams->receiverName:"); | |
Toastr::success('Sample Task Successfully Saved', 'Success'); | |
return redirect()->route('admin.sampletask.index'); | |
} | |
} |
now you should check you have a jobs table in your database. for this you should create a migration by putting below command
php artisan queue:table
For more reference see this article Click Here
Now you should run a command in terminal for processing queues:
php artisan queue:work
after this whenever you click on submit button in your form your form will submitted within 2 to 3 second and your email will process in the background and your request of email should be partially stored in jobs table.
for more details you can refer below articles:
Click Here: reference of queue



MotoShare.in provides the perfect two-wheeler for every occasion—daily commuting, weekend escapes, tourist exploration, or test-riding your dream bike. Simplify your mobility with us!