Reputation: 31
I am using laravel 9.19 version an
While the job of sending mail with the cron I have defined is working successfully with the cron I have defined to the panel of the hosting company below
'cd /home/customer/www/myproject.webaddress.com/public_html && php artisan schedule:run > /dev/null 2/&1'
I changed the definition above and followed the steps below I defined the following cron of the hosting company's panel
'cd /home/customer/www/myproject.webaddress.com/public_html && php artisan schedule:work > /dev/null 2/&1'
then under laravel directory with ssh 'php artisan schedule:work' I ran it. Then I logged out with ctrl+c.
After doing the above steps, 1 incoming cron mail x16 started to arrive. Even after deleting the cron in the hosting company's panel, it sends the same mail x16 times as long as the mail sending code is active in Laravel. How can I stop this?
app/console/kernel.php file schedule() method:
protected function schedule(Schedule $schedule)
{
$schedule->command('send:contractremindermail')->everyMinute()->emailOutputOnFailure('[email protected]');
//If I don't comment here, x16 mail continues to be sent.
}
app/Mail/ContractReminderMail.php build method()
public function build()
{
$temp = config('myconfig.contract_finish_interval');
switch ($temp) {
case 'day':
$contract_finish_interval = __('1gun');
break;
case 'week':
$contract_finish_interval = __('1hafta');
break;
case 'month':
$contract_finish_interval = __('1ay');
break;
case 'quarter_year':
$contract_finish_interval = __('3ay');
break;
case 'half_year':
$contract_finish_interval = __('6ay');
break;
case 'year':
$contract_finish_interval = __('1yil');
break;
default:
break;
}
return $this->subject(__('bitimi_yaklasan_sozlesmeler'))
->view('cron_emails.contract_reminder_mail')
->with([
'contracts' => $this->contracts,
'contract_finish_interval' => $contract_finish_interval,
]);
}
app/Console/Commands/SendContractReminderMail.php handle() method
public function handle()
{
$contract_finish_interval = config('myconfig.contract_finish_interval');
$day = 1;
switch ($contract_finish_interval) {
case 'day':
$day = $day * 1;
break;
case 'week':
$day = $day * 7;
break;
case 'month':
$day = $day * 31;
break;
case 'quarter_year':
$day = $day * 92;
break;
case 'half_year':
$day = $day * 182;
break;
case 'year':
$day = $day * 365;
break;
default:
break;
}
$contracts = DB::table('contracts')
->join('contract_types', 'contract_types.id', '=', 'contracts.contracttype_id')
->join('companies', 'companies.id', '=', 'contract_types.company_id')
->join('customers', 'customers.id', '=', 'contracts.customer_id')
->select(
'contracts.*',
'companies.name as company_name',
'companies.id as company_id',
'companies.status as company_status',
'contract_types.name as contracttype_name',
'contract_types.status as contracttype_status',
'customers.phone as phone',
)
->where('end', '<=', Carbon::now()->addDays(intval($day))->isoFormat('YYYY-MM-DD'))
->orderBy('end', 'asc')
->get();
Mail::to(config('myconfig.reminder_email_address'))->send(new ContractReminderMail($contracts));
return 0;
}
resources/cron_emails/contract_reminder_mail.blade.php
<style>
table,
th,
tr,
td {
border: 1px solid #24695c;
border-collapse: collapse;
}
th,
tr,
td {
padding: 10px;
}
</style>
<h1>{{ __('bitimi_yaklasan_sozlesmeler') }}</h1>
<p>{{ __('merhabalar') }}</p>
<p>{{ __('belirttiginiz_kriterlere_gore_sistemde_kayitli_bitimi_yaklasan_sozlesmeler_asagidaki_tablodadir') }}<i><small>({{ __($contract_finish_interval) }})</i></small>
</p>
<table>
<tr>
<th>{{ __('musteri') }}</th>
<th>{{ __('telefon') }}</th>
<th>{{ __('baslangic_tarihi') }}</th>
<th>{{ __('bitis_tarihi') }}</th>
<th>{{ __('bitime_kalan_gun') }}</th>
<th>{{ __('sozlesme_turu') }}</th>
<th>{{ __('sirket') }}</th>
<th>{{ __('sozlesme_no') }}</th>
<th>{{ __('aciklama') }}</th>
</tr>
@foreach ($contracts as $item)
@if (floor(strtotime($item['end']) - strtotime(date('d-m-Y'))) / (60 * 60 * 24) >= 0)
<tr>
<td>{{ $item['customer'] }}</td>
<td>{{ $item['phone'] }}</td>
<td>{{ $item['start']}}</td>
<td>{{ $item['end'] }}</td>
<td>{{ floor(strtotime($item['end']) - strtotime(date('Y-m-d'))) / (60 * 60 * 24) . ' ' . __('gun') }}</td>
<td>{{ $item['contracttype_name'] }}</td>
<td>{{ $item['company_name'] }}</td>
<td>{{ $item['contract_number'] }}</td>
<td>{{ $item['description'] }}</td>
</tr>
@else
<?php continue; ?>
@endif
@endforeach
</table>
Upvotes: 1
Views: 676
Reputation: 31
i am fixed with append adding to the end ->withoutOverlapping()
protected function schedule(Schedule $schedule)
{
$schedule->command('send:contractremindermail')->everyMinute()->emailOutputOnFailure('[email protected]')->withoutOverlapping();
}
But I would like to know in detail why I need it.
Upvotes: 2