使用Laravel在特定日期发送提醒电子邮件 [英] Send reminder email at certain date using Laravel

查看:129
本文介绍了使用Laravel在特定日期发送提醒电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施提醒电子邮件",该电子邮件将在特定日期发送;

I would like to implement "reminder email" that will be sent on a certain date;

迁移:

Schema::create('todos', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('importance');            
    $table->date('when');
    $table->timestamps();
    $table->string('to');
});

(电子邮件地址将是创建此提醒的用户的地址).

(email address will be the address of the user who created this reminder).

命令:

<?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不存在".

The issue is, when I try php artisan email:reminder, I receive error message "Class App\Console\Commands\Request does not exist".

我真的很困惑,确实在StackOverlow上检查了类似的问题并在Google上进行了搜索,但无法弄清楚.感谢您的帮助.

I'm really puzzled and did check similar questions here on StackOverlow and googled it, but can't manage to figure it out. Any help is appreciated.

推荐答案

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

Try adding use Request; to the imports at the beginning of your console command's file. Because Request is a facade and it belongs to the root namespace or, put a \ before its every usage.

这篇关于使用Laravel在特定日期发送提醒电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆