Queue Delay , Send Emails with Delay in Laravel 10

Queue Delay , Send Emails with Delay in Laravel 10

14-Sep-2024
| |
Image Carousel

Hello developers in this tutorial we will discuss about how to add delay while sending emails in a queue

Table of Contents

S.no Contents-topics
1 Basic env setup for sending emails
2 Configuration for job-queues
3 Making job-queues
4 Adding delay to job
5 Calling the job queues

1:Basic env setup for sending emails

For sending emails we first .env file with mail credentials like mail driver , hostname,port number

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="DeveloperCodez"

2:Configuration for job-queues

For creating new job in brief you may consider JOBS IN LARAVEL 10 , JOBS-QUEUES IN LARAVEL 10  now we will create jobs table in database which stores jobs and process them one by one with FCFS( first come first serve).
Copy for creating tableCopy

php artisan queue:table

It will create migration file and then we have to create this migration in our database
command for migartion  Copy

php artisan migrate

3:Making job-queues

 This will create Jobs table in database
Now we will create our new job with name  SendEmailToUser

command for job: Copy

php artisan make:job SendEmailToUser

Now for the testing the jobs in laravel we will change the name of the users in App\Jobs\SendEmailToUser with the help of jobs , paste the following code in app/Jobs/SendEmailToUser to update the user
code for SendEmailToUser
Copy

<?php
 
namespace App\Jobs;
 
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;
use Exception;
 
class SendEmailToUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public $desc;
    public $type;
    public $emails;
    public $country;
    public $subject;
 
    public function __construct($emails)
    {
 
        $this->emails = $emails;
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
 
        $emails = $this->emails;
 
        foreach ($emails as $list) {
 
            try {
                $data  = ['name' => $list->name];
                $user['to'] = $list->email;
                Mail::send('welcomeMail', $data, function ($messages) use ($user) {
                    $messages->to($user['to']);
                    $messages->subject('Delay in mail');
                });
            } catch (Exception $e) {
                // add error
                continue;
            }
 
           
         
        }
    }
}

create a view welcomeMail in resources/views which will send in the mail

 in .env file make QUEUE_CONNECTION=sync to QUEUE_CONNECTION=database
.env  Copy

QUEUE_CONNECTION=database

4: Adding delay to the job

Now from controller we call this SendEmailToUser job 
code: Copy

use App\Jobs\SendEmailToUser;
   public function sendEmailNewsletter(Request $request)
    {
        $emails = User::get();
       
        if(isset($emails[0])){
            $job = (new SendEmailToUser($emails)) ->delay(now()->addSeconds(2));
            dispatch($job);
 
           
           
        }
 
       return Redirect::back();
    }

new SendEmailToUser($emails)) ->delay(now()->addSeconds(2) this method will add delay of 2 seconds in sending mail 

5:Calling the job-queues

Now hit following command in your terminal 
command:Copy

 php artisan queue:work

Tags: queue delay , send emails with delay in laravel 10 , send email by using job with delay in laravel 10 , send queue job with adding delay in laravel 10,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter