首页 > 解决方案 > Laravel 5,Mail:: with later() 不工作

问题描述

Mailwithlater()不适用于我将其作为参数提供的时间。

$when = $final_new_days_count;//Carbon::now()->addMinutes(5);
Mail::to($user)
    ->later($when, new Expired($user, $when));

I'm receiving the email just when I execute the code that calls `Mail::`

With `dd($when);` I get:

Carbon {#766 ▼
  +"date": "2019-07-22 23:59:59.229078"
  +"timezone_type": 3
  +"timezone": "UTC"
}

Actually, in the email view I'm sending this:

<p>Now: {{ \Carbon\Carbon::now() }}</p>
<p>When: {{ $when->date }}</p>

我在收到的电子邮件中看到:

Now: 2019-07-19 16:18:23
When: 2019-07-22 23:59:59.315271

那么为什么不工作later呢?

编辑:在我的 .env 中,我有:

QUEUE_DRIVER=sync

我不能改变它,其他一些功能需要它。

编辑 2:好吧,我用 QUEUE_DRIVER=database 进行了测试,但不知道 php artisan queue:work 必须一直运行。我不能一直运行这个:S

标签: laravel

解决方案


Laravel 邮件later功能需要一个队列才能运行。默认情况下,您QUEUE_DRIVER设置为sync. 这意味着它将立即触发它。

使用redisordatabase队列,以便它可以将作业排队以备后用。

QUEUE_DRIVER=redis

如果您不想一直php artisan queue:work在命令行中运行,我建议您使用像 Supervisor 这样的流程管理器。

关于队列进程和进程管理器的文档:

https://laravel.com/docs/5.8/queues#running-the-queue-worker

安装主管:

https://cloudwafer.com/blog/how-to-install-and-configure-supervisor-on-ubuntu-16-04/

这将允许php artisan queue:work作为后台进程运行。

如果您决定使用主管,以下配置将起作用:

 `[program:laravel-worker]
  process_name=%(program_name)s_%(process_num)02d
  command=php /path/to/artisan queue:work
  autostart=true
  autorestart=true
  numprocs=8
  redirect_stderr=true
  stdout_logfile=/home/forge/app.com/worker.log`

推荐阅读