首页 > 解决方案 > 使用 laravel 的 cron 作业向用户发送电子邮件

问题描述

请帮助我,我正在尝试在我的服务器上进行 cron 作业以向用户发送电子邮件我创建了这样的命令:

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'contract:end';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send an email to user about the end of a contract';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $contracts = Contract::where('end_date', Carbon::parse(today())->toDateString())->get();

        foreach ($contracts as $contract) {
            Mail::to(Setting::first()->value('email'))->send(new ContractEmail($contract));
        }
    }

在我的kernel.php我添加了以下代码:

  * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        Commands\ContractEnd::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('contract:end')
                  ->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

在我的服务器上,我运行以下命令来运行 cron 作业:

cd /archive/artisan && schedule:run >> /dev/null 2>&1   

但它没有发送任何电子邮件,任何人都可以帮助我,因为这是我第一次使用它?

标签: laravelcron

解决方案


因为您在服务器中的命令是错误的,您需要指定正确的路径,该路径将包含/home/您的用户名/您的网站文件夹/artisan 像这样 /usr/local/bin/php /home/your user name which you can get in from job page/archive/artisan schedule:run 1>> /dev/null 2>&1


推荐阅读