ali sam
ali sam

Reputation: 1

how can send multiple mail with cronjobs using laravel?

cron jobs run hourly and daily automatic is not working

hourlyemail.php

 <?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Jobs\IntervalTaskCreationMailJob;
use DB;
class HourlyMail extends Command
{
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'HourlyMail:email';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Hourly mail sending by admin';

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

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $hourly =DB::table('regular_tasks')
        ->leftJoin('regular_task_user','regular_tasks.id', '=','regular_task_user.regular_task_id')
        ->leftJoin('users','regular_task_user.user_id', '=','users.id')
        ->select('regular_tasks.*','users.email')
        ->where('regular_tasks.task_freq', 'hourly')->get();          
        
        foreach($hourly as $d){
        $demail = $d->email;
        IntervalTaskCreationMailJob::dispatch($d,$demail)
        ->delay(now()->addSeconds(10));
        }
               
               
 }
 }

mail.php

<?php

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

class HourlySendMail extends Mailable
{
use Queueable, SerializesModels;  
public $d;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($d)
{
    $this->d = $d;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from('[email protected]')
    ->subject('Hourly Mail from Devops Team')
    ->view('mails.intervaltaskcreationmail')
    ->with('d', $this->d);
}
}

0

How i can run my Schedule task every minute, hourly and daily?. When I run command PHP artisan schedule: run, console get "No scheduled commands are ready to run.". How should set my Tasks properly? For example if I use solo command php artisan command:send_info_email_before_event it works, but i want do it with schedule:run when i develop app.

Upvotes: 0

Views: 571

Answers (1)

zlatan
zlatan

Reputation: 3951

Since you already created your cron job, you can schedule it in the app/Console/Kernel.php file, like this:

protected function schedule(Schedule $schedule)
{
    $schedule->command('HourlyMail:email')->daily();
}

Instead of daily(), you have plenty of other methods which you can use for your task scheduling. You can see all of them on official documentation.

Upvotes: 1

Related Questions