首页 > 解决方案 > 使用 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 上检查了类似的问题并用谷歌搜索了它,但无法弄清楚。任何帮助表示赞赏。

标签: phplaravel

解决方案


尝试use Request;在控制台命令文件的开头添加导入。因为Request是一个门面,它属于根命名空间,或者,\在它的每次使用之前都放一个。


推荐阅读