How to send email with schedule queue in laravel?

Member

by adan , in category: PHP Frameworks , 3 months ago

How to send email with schedule queue in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , 3 months ago

@adan 

To send an email with a scheduled queue in Laravel, follow these steps:

  1. Create a new Mailable class by running the following command:
1
php artisan make:mail NewEmail


  1. Edit the NewEmail class to define the email template and data to be passed to the view.
  2. Create a new job class by running the following command:
1
php artisan make:job SendScheduledEmail


  1. Edit the SendScheduledEmail class to send the email using the Mailable class created in step 2.
  2. Use the Queue facade to dispatch the job with a delay. For example, to send the email in 1 hour, use the following code:
1
Mail::to($email)->queue((new NewEmail())->delay(now()->addHour()));


  1. Update your .env file to point to a queue driver that supports delayed jobs, such as redis or database.
  2. Start your queue worker by running the following command:
1
php artisan queue:work


Your email will now be sent with a scheduled queue in Laravel.