Home > front end >  Cronjob Keeps Sending Multiple Emails
Cronjob Keeps Sending Multiple Emails

Time:12-09

I've been developing a website where users create complaints about brands. The problem is that I want to send the users a reminder email and notification after 3 days of creating their complaints. And I want to send this email once. That's why I checked if the reminder has been sent before.

This code works fine on localhost and sends only one email and notification. But it keeps sending multiple emails and notifications (sometimes 40-50) in a minute on Ubuntu server. The code works multiple times without executing codes down below. Does anyone know why I have this problem?

COMPLAINTREMINDER.php:

protected $signature = 'complaints:reminder';

public function handle()
{
    $complaints = Complaints::with('author')->whereIn('status', ['now_active', 'answered'])->where('approved_at', '<=', Carbon::now()->subHour(72))->get();

    foreach($complaints as $complaint) {
        $subject = "random subject";
        $text = "randomtext";
        $textPanel = "Text for notification panel";

        if ($complaint->is_reminder_sent == 0) {
            $this->sendMailByZoho($complaint->author->email, $subject, $text);
            $this->sendPanelNotification($complaint->author, $textPanel, 'memberNewComplaint', 'type');
            $complaint->update(['is_reminder_sent' =>  '1']);
        }
    }

    return 0;
}

KERNEL.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('complaints:reminder')->everyMinute();
}

CRONTAB:

* * * * * cd /path_to_my_project && php artisan schedule:run >> /dev/null 2>&1

CodePudding user response:

$complaint->update(['is_reminder_sent' =>  '1']);

Is this line working?

I would have used:

$complaint->is_reminder_sent = 1;
$complaint->save();

CodePudding user response:

I solved the problem. Updating cron worked for me.

  • Related