Laravel 5.6 如何安排邮件队列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50198003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:42:19  来源:igfitidea点击:

Laravel 5.6 How to schedule email queue

phplaravelscheduled-taskstask-queuelaravel-5.6

提问by Wei

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:

我正在尝试安排一封电子邮件来提醒明天有待完成任务的用户。我做了一个自定义命令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('[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

我现在对电子邮件进行了硬编码以对其进行测试,但是当我运行时php artisan email:reminder,我得到了一个例外

[InvalidArgumentException]     
  Only mailables may be queued.

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

然后我检查了 Laravel 的文档,但任务调度和电子邮件队列是 2 个独立的主题。

  • 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?
  • 请问如何在 Laravel 5.6 中通过任务调度实现发送邮件队列?
  • 另外,我如何将数据,即数据库中的待办事项传递到我的电子邮件视图中?

Any help is greatly appreciated!

任何帮助是极大的赞赏!

采纳答案by DigitalDrifter

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

使用控制台内核来安排排队的作业很容易做到。Laravel 提供了几种包装方法,使 cron 集成变得微不足道。这是一个基本示例:

$schedule->job(new SendTodoReminders())->dailyAt('9:00');

回答by Leroy

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.

您应该创建一个完全按照您描述的方式执行的命令,但没有调度。您可以使用 crontab 进行调度或其他一些任务调度程序。

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

您是否遵循了 Laravel 关于邮件的文档?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.

当该命令起作用时,将其添加到任务调度程序(例如 crontab)以每天运行。

回答by Adnan Mumtaz

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

is deprecated since Laravel 5.3. Only the Mailables can Queued and it should implement the ShouldQueue interface.

自 Laravel 5.3 起已弃用。只有 Mailables 可以 Queued 并且它应该实现 ShouldQueue 接口。

For running jobs you have to configure the queue driverand run php artisan queue:work

要运行作业,您必须配置queue driver并运行php artisan queue:work