php - 使用 Laravel 在特定日期发送提醒邮件
问题描述
我想实现将在某个日期发送的“提醒电子邮件”;
移民:
Schema::create('todos', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('importance');
$table->date('when');
$table->timestamps();
$table->string('to');
});
(电子邮件地址将是创建此提醒的用户的地址)。
命令:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Todo;
use Illuminate\Support\Facades\Mail;
use App\Mail\EmailReminder;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'email:reminder';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send reminder e-mails to a users';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() //Todo $todo
{
parent::__construct();
//$this->todo = $todo;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(Request $request, $id, $todo)
{
$i = 0;
$todo = Todo::whereMonth('when', '=', date('m'))->whereDay('when', '=', date('d'))->get();
foreach($todo as $todo)
{
$email = $todo->email;
Mail::to($email)->send(new BirthdayReminder($todo));
$i++;
}
}
}
可邮寄:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Todo;
class EmailReminder extends Mailable
{
use Queueable, SerializesModels;
public $todo;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Todo $todo)
{
$this->todo = $todo;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$todo = $this->todo;
return $this->from('friendlyreminder@yourcompany.rs')
->view('emails.reminder',compact('todo'));
//Mail::to(Auth::user()->email)->send(new EmailReminder());
}
}
核心:
protected function schedule(Schedule $schedule)
{
$schedule->command('email:reminder')->everyMinute();
}
问题是,当我尝试使用 php artisan email:reminder 时,我收到错误消息“Class App\Console\Commands\Request 不存在”。
我真的很困惑,并在 StackOverlow 上检查了类似的问题并用谷歌搜索了它,但无法弄清楚。任何帮助表示赞赏。
解决方案
尝试use Request;
在控制台命令文件的开头添加导入。因为Request
是一个门面,它属于根命名空间,或者,\
在它的每次使用之前都放一个。
推荐阅读
- javascript - 最小化窗口/浏览器选项卡时 setInterval 函数无法正常工作
- vector - 使用 mmapLayerComboBox 中的选定图层
- swift - 工作表和警报不断刷新我的 WKWebView SwiftUI 页面
- python - 每个进程的速度随着更多进程而变慢
- python - 如何在 keras 中使用 elmoformanylangs?
- tomcat - 如果我调用 connector.start(),Tomcat 9 会抱怨启动两次?
- javascript - CryptoJS 解密值与原始值不同
- prolog - 尝试获取组合时参数没有充分实例化
- javascript - 我如何获得一个 wordpress 插件以使来自前端的访问者能够按升序或降序对帖子进行排序
- python - 当本地模块中的代码发生更改时,我是否总是必须在 Jupyter Lab 中重新启动内核?