Laravel 5.6 如何安排电子邮件队列
Laravel 5.6 How to schedule email queue
我正在尝试安排一封电子邮件来提醒明天有待办事项的用户。我做了一个自定义命令 email:reminder
。这是我在自定义命令中的代码:
<?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('example@email.com')
->from('todoreminder@gmail.com', 'To-do Reminder')
->subject('Due tomorrow on your To-do list!');
}
);
$this->info('Reminder email sent successfully!');
}
}
我暂时对电子邮件进行了硬编码以对其进行测试,但是当我 运行 php artisan email:reminder
时,我得到了
的异常
[InvalidArgumentException]
Only mailables may be queued.
然后我查看了 Laravel 的文档,但任务计划和电子邮件队列是两个不同的主题。
- 请问如何在Laravel
5.6中实现带任务调度的邮件队列发送?
- 另外,我如何将数据(即数据库中的待办事项)传递到我的电子邮件中
请查看?
非常感谢任何帮助!
您应该创建一个完全按照您描述的方式执行的命令,但没有计划。您可以使用 crontab 进行调度或使用其他一些任务调度程序。
您是否遵循了 Laravel 关于邮寄的文档? https://laravel.com/docs/5.6/mail
一旦你到达Sending Mail section,你不应该创建一个控制器,而是一个命令。
当该命令起作用时,每天将其添加到任务计划程序(例如 crontab)到 运行。
Mail::queue('emails.reminder', [], function ($mail) {
$mail->to('example@email.com')
->from('todoreminder@gmail.com', 'To-do Reminder')
->subject('Due tomorrow on your To-do list!');
}
);
自 Laravel 5.3 后已弃用。只有 Mailables 可以排队,它应该实现 ShouldQueue 接口。
对于 运行ning 作业,您必须配置 queue driver
和 运行 php artisan queue:work
使用控制台内核 schedule queued jobs 很容易做到。 Laravel 提供了几种使 cron 集成变得微不足道的包装器方法。这是一个基本示例:
$schedule->job(new SendTodoReminders())->dailyAt('9:00');
我正在尝试安排一封电子邮件来提醒明天有待办事项的用户。我做了一个自定义命令 email:reminder
。这是我在自定义命令中的代码:
<?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('example@email.com')
->from('todoreminder@gmail.com', 'To-do Reminder')
->subject('Due tomorrow on your To-do list!');
}
);
$this->info('Reminder email sent successfully!');
}
}
我暂时对电子邮件进行了硬编码以对其进行测试,但是当我 运行 php artisan email:reminder
时,我得到了
[InvalidArgumentException]
Only mailables may be queued.
然后我查看了 Laravel 的文档,但任务计划和电子邮件队列是两个不同的主题。
- 请问如何在Laravel 5.6中实现带任务调度的邮件队列发送?
- 另外,我如何将数据(即数据库中的待办事项)传递到我的电子邮件中 请查看?
非常感谢任何帮助!
您应该创建一个完全按照您描述的方式执行的命令,但没有计划。您可以使用 crontab 进行调度或使用其他一些任务调度程序。
您是否遵循了 Laravel 关于邮寄的文档? https://laravel.com/docs/5.6/mail
一旦你到达Sending Mail section,你不应该创建一个控制器,而是一个命令。
当该命令起作用时,每天将其添加到任务计划程序(例如 crontab)到 运行。
Mail::queue('emails.reminder', [], function ($mail) {
$mail->to('example@email.com')
->from('todoreminder@gmail.com', 'To-do Reminder')
->subject('Due tomorrow on your To-do list!');
}
);
自 Laravel 5.3 后已弃用。只有 Mailables 可以排队,它应该实现 ShouldQueue 接口。
对于 运行ning 作业,您必须配置 queue driver
和 运行 php artisan queue:work
使用控制台内核 schedule queued jobs 很容易做到。 Laravel 提供了几种使 cron 集成变得微不足道的包装器方法。这是一个基本示例:
$schedule->job(new SendTodoReminders())->dailyAt('9:00');