1) php artisan make:command AutoBirthDayWish
2) php artisan make:mail BirthDayWish
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class BirthDayWish extends Mailable
{
use Queueable, SerializesModels;
public $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Happy Birthday '. $this->user->name)
->view('emails.birthdayWish');
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Mail;
use App\Mail\BirthDayWish;
use App\Models\User;
class AutoBirthDayWish extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'auto:birthdaywith';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$users = User::whereMonth('birthdate', date('m'))
->whereDay('birthdate', date('d'))
->get();
if ($users->count() > 0) {
foreach ($users as $user) {
Mail::to($user)->send(new BirthDayWish($user));
}
}
return 0;
}
}
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('auto:birthdaywith')->daily();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Saturday, October 15, 2022
Schedule/Auto Email sending from Laravel (Cron Job)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment