In your command, you just check the date (for example : user birthdate, subscription date, ...) and if the condition is ok, you send the email.
Jun 25, 2022
17
Level 1
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?
Please or to participate in this conversation.