Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.6 How to schedule email queue

I'm trying to schedule an email to remind users who have to-do tasks due tomorrow. I made a custom command email:reminder. Here is my code in custom command:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Todo;
use Illuminate\Support\Facades\Mail;

class SendReminderEmail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'email:reminder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Remind users of items due to complete next day';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        /*
         * Send mail dynamically
         */

        /*
         * hardcoded email
         */
        Mail::queue('emails.reminder', [], function ($mail) {
            $mail->to('[email protected]')
                ->from('[email protected]', 'To-do Reminder')
                ->subject('Due tomorrow on your To-do list!');
        }
        );


        $this->info('Reminder email sent successfully!');
    }
}

I hardcoded the email for now to test it, but when I ran php artisan email:reminder, I got an exception of

[InvalidArgumentException]     
  Only mailables may be queued.

I then checked Laravel's documentation, but Task Scheduling and Email Queue are 2 separate topic.

  • How can I achieve sending email queue with task scheduling in Laravel 5.6 please?
  • Also how can I pass data, i.e. to-do items in database into my email view please?

Any help is greatly appreciated!

like image 998
Wei Avatar asked May 06 '18 09:05

Wei


People also ask

How to configure queue in Laravel?

A regular queue in Laravel consists of a job, a logic for the task you want to run, and a worker that runs the job. All the data about queues configuration is stored in config/queue.php. Also, this file contains connection configurations for each queue driver. Connections may have several assigned queues.

How to create email in Laravel 6 using Laravel mail?

first of all we need to get fresh Laravel 6 version application using bellow command, So open your terminal OR command prompt and run bellow command: Step 2: Create Mail Setup We are going from scratch and in first step, we will create email for testing using Laravel Mail facade.

How do I create a schedule in Laravel?

Laravel's command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. When using the scheduler, only a single Cron entry is needed on your server. Your task schedule is defined in the app/Console/Kernel.php file's schedule method.

How do I use $schedule->call() with a queue?

Using $schedule->call () looses the ability to specify a queue name, and other job specific parameters. To retain this, you should use the schedule job command, which can be chained as follows: $schedule->job ( (new firstJob ())->chain ( [ new secondJob (), new thirdJob () ]), 'queue-name')->everyFiveMinutes ();


2 Answers

Using the console kernel to schedule queued jobs is easy to do. Laravel offers several wrapper methods that make the cron integration trivial. Here's a basic example:

$schedule->job(new SendTodoReminders())->dailyAt('9:00');
like image 145
Brian Lee Avatar answered Oct 06 '22 17:10

Brian Lee


You should create a command which does exactly as you described, but without the scheduling. You can use the crontab for scheduling or some other task scheduler.

Have you followed Laravel's documentation about mailing? https://laravel.com/docs/5.6/mail

Once you get to the Sending Mail section, you should not create a controller but a command instead.

When that command works, add it to the task scheduler (eg. crontab) to run on a daily basis.

like image 42
Leroy Avatar answered Oct 06 '22 16:10

Leroy