Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kumari_shwetha's avatar

How to send a scheduled email based on a date for the user using Laravel 8?

I want to send 7 days prior to the user, Theirs Selected plan is going to expire. I have implemented using command and cron jobs to run the command. But it is not working. Please anybody guide me to correct my mistake?

app/Console/Commands/AutoPlanRenewalMail.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Mail;
use App\Mail\PlanRenewalMail;
use App\User;

class AutoPlanRenewalMail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'auto:planrenewal';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $users = User::whereNotNull('plan_validity')->get();
        $check = true;
        foreach($users as $user){
            if(Carbon::parse($user->plan_validity)->diffInDays(Carbon::now()) == 7){ //Or however your date field on user is called
              
                    Mail::to($user)->send(new PlanRenewalMail($user));
            }
        }
    
        return 0;
    }
}

app/Console/kernal.php

<?php

namespace App\Console;

use App\Console\Commands\AutoPlanRenewalMail;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        AutoPlanRenewalMail::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
         $schedule->command('auto:planrenewal')->daily();
    }

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

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

app/Mail/PlanRenewalMail.php


<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PlanRenewalMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        //return $this->view('view.name');
        return $this->subject("{{ ENV('APP_NAME') }} EXPIRING")
                    ->view('emails.plan-renewal-mail');
    }
}

cron jobs

* * * * * /usr/local/bin/php /home/username(changed to my username)/public_html/artisan schedule:run >> /dev/null 2>&1

then included email template blade file. But any email didn't get still. Is there any coding errors?

0 likes
17 replies
vincent15000's avatar

In your command, you just check the date (for example : user birthdate, subscription date, ...) and if the condition is ok, you send the email.

Kumari_shwetha's avatar

@vincent15000

 public function handle()
    {
        $users = User::whereNotNull('plan_validity')->get();
        $check = true;
        foreach($users as $user){
            if(Carbon::parse($user->plan_validity)->diffInDays(Carbon::now()) == 7){ 
              
                    Mail::to($user)->send(new PlanRenewalMail($user));
            }
        }
    
        return 0;
    }
1 like
Snapey's avatar

does it work when you run the command manually?

1 like
Kumari_shwetha's avatar

@Snapey as it is in cpnel server, written in cron jobs

* * * * * /usr/local/bin/php /home/hellovcard/public_html/artisan schedule:run >> /dev/null 2>&1

1 like
Snapey's avatar

@Kumari_shwetha not what i asked

by the way, i often add a simple Log entry in my kernel that runs every 10 minutes and just says 'scheduler running' so that I am confident that my cron is ok

1 like
vincent15000's avatar

@Kumari_shwetha If your issue is solved, please close the post by setting the best answer to the answer that helped you to solve your issue ;).

Please or to participate in this conversation.