0

I need to send email using jobs.

First step: I create tables using migrations(tables: jobs and failed_jobs).

Second step: I create class with method hendle using console command:

php artisan make:job SendForgotPasswordEmail

Thirt step: in method hendle() I put

Mail::to($this->user)->send(new RefreshPassword($this->user));

Fourth step: in my controller:

dispatch((new SendForgotPasswordEmail($user))->delay(Carbon::now()->addMinutes(2)));

Fifth step:

php artisan queue:work

My queue.php file:

'default' => env('QUEUE_DRIVER', 'sync'),

'connections' => [

    'sync' => [
        'driver' => 'sync',
    ],

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],

    'beanstalkd' => [
        'driver' => 'beanstalkd',
        'host' => 'localhost',
        'queue' => 'default',
        'retry_after' => 90,
    ],

    'sqs' => [
        'driver' => 'sqs',
        'key' => 'your-public-key',
        'secret' => 'your-secret-key',
        'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-account-id',
        'queue' => 'your-queue-name',
        'region' => 'us-east-1',
    ],

    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => 'default',
        'retry_after' => 90,
    ],

],

'failed' => [
    'database' => env('DB_CONNECTION', 'mysql'),
    'table' => 'failed_jobs',
],

And job:

 public function __construct($user)
{
    $this->user = $user;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    Mail::to($this->user)->send(new RefreshPassword($this->user));
}

Then I sent email. But email sent momently, although in method delay I put 2 minutes. Thank's.

1 Answer 1

1

I had the same issue and replaced

dispatch((new SendForgotPasswordEmail($user))->delay(Carbon::now()->addMinutes(2)));

with

Queue::later(120, (new SendForgotPasswordEmail($user)));

and it worked

Sign up to request clarification or add additional context in comments.

6 Comments

thank you, but the same problem, and I don't understand why
the record didn't push in table
can you show the queue.php and SendForgotPasswordEmail?
Yes. I put in the main message
ok replace the default attribute from env('QUEUE_DRIVER', 'sync') to database. Also, make sure that the SendForgotPasswordEmail class implements ShouldQueue and uses the traits InteractsWithQueue and DispatchesJobs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.