使用Laravel在特定日期发送提醒电子邮件

时间:2018-10-28 13:14:39

标签: 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上检查了类似的问题并用谷歌搜索了它,但是无法弄清楚。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

尝试将use Request;添加到控制台命令文件开头的导入中。 因为Request是一个外观,并且它属于根名称空间,所以,请在每次使用\之前放置它。