Hello, I am working on an email sender to our users using Laravel queue, it works great but there is one issue which I am struggling with which is it takes too much time to create jobs for 10k users we have in our database, we are ok with waiting for it to work but we need to show some kind of progress in percentage on the page like the upload progress. Here is the code I am using, please let me know how can we make it to insert into database faster or if possible show the progress.
try {
DB::beginTransaction();
$email_log = new EmailLog;
$email_log->sender_name = $sender_name;
$email_log->sender_email = $sender_email;
$email_log->subject = $this->subject;
$email_log->message = $this->message;
$email_log->save();
foreach ($this->recipients as $recipient) {
$details = [
"from" => $sender_email,
"from_name" => $sender_name,
"to" => $recipient->email,
"subject" => convertEmailTemplate($recipient, 'email sender', $this->subject),
"message" => convertEmailTemplate($recipient, 'email sender', $this->message),
"subcopy" => "To login please <a href='" . route('recipient.login') . "?token=" . Crypt::encryptString($recipient->token) . "'>click here</a>",
];
Mail::queue(new MailSender($details));
$email_log_detail = new EmailLogDetail;
$email_log_detail->email_log_id = $email_log->id;
$email_log_detail->recipient_id = $recipient->id;
$email_log_detail->event_type = 'sent';
$email_log_detail->data = json_encode($details);
$email_log_detail->save();
}
} catch (\Exception $e) {
$errors = true;
DB::rollBack();
$this->dispatchBrowserEvent('error_popup', ['type' => 'error', 'message' => $e->getMessage()]);
}
if (!$errors) {
DB::commit();
$this->dispatchBrowserEvent('success_popup', ['type' => 'success', 'message' => "Emails queued for dispatch."]);
}
Thank you