lemmon's avatar
Level 28

Queued mail sends automatically

Hello all, when I send mail using queue the mail just sends. it does not wait for me to enter artisan mail:work?

0 likes
6 replies
LaryAI's avatar
Level 58

Certainly! Here’s a solution to your issue:

When you queue a mail in Laravel, it gets pushed onto the queue driver you have configured in your .env file. If your mail is being sent immediately, it’s likely because your queue driver is set to sync. The sync driver processes jobs immediately, rather than waiting for a worker.

How to fix:

  1. Check your queue driver in .env:

    QUEUE_CONNECTION=sync
    

    If it says sync, change it to a driver that supports background processing, such as database, redis, or beanstalkd. For example:

    QUEUE_CONNECTION=database
    
  2. Run the queue worker:

    After changing the driver, you need to run the queue worker to process queued jobs:

    php artisan queue:work
    

    or

    php artisan queue:listen
    
  3. Migrate the jobs table (if using database driver):

    If you use the database driver, make sure you have the jobs table:

    php artisan queue:table
    php artisan migrate
    

Summary:
Your mails are being sent immediately because your queue driver is set to sync. Change it to a real queue driver and run the worker to process jobs in the background.

References:

Let me know if you need more details!

lemmon's avatar
Level 28

@LaryAI need more info. the queue connection is set to database and the jobs table is there.

lemmon's avatar
Level 28

The QUEUE_CONNECTION is set to database in the config and in the .env file but the mail automatically sends anyway without using queue:listen or queue:work almost as if for some reason the worker is started ?

lemmon's avatar
Level 28

The composer run dev command also runs queue:work and queue:listen for you so I used the generic php artisan serve and that solved the dilemma I was having. Thanks.

Snapey's avatar
Snapey
Best Answer
Level 122

more likely schedule:work and queue:listen

Please or to participate in this conversation.