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.