sending daily summary email

  laravel, php

I am writing up a cron job for daily email notification.

Here are the scenario lets say

  1. User A gets 10 leads a day.
  2. User B gets 5 leads a day.
  3. User C gets 2 leads a day.

i want to send one email to User A having 10 leads information in it.

then one email to User B having 5 leads information in it.

then one email to User C having 2 leads information in it.

so I want to try to create a summary email having lead information in it for a particular user.

   foreach ($today_leads as $today_lead) {

            $data[] = [
                'user_id' => $today_lead->user_id,
                'user_fullname' => $today_lead->user_fullname,
                'user_email' => $today_lead->user_email,
                'lead_firstname' => $today_lead->first_name,
                'lead_lastname' => $today_lead->last_name,
                'lead_email' => $today_lead->email,
                'lead_phone' => $today_lead->phone,
                'lead_source' => $today_lead->source,
            ];
                
            Mail::to(data['user_email'])->send(new DailyLeadSummary($data));

        }

if I write my in foreach loop then I end up sending 10 emails to User A, 5 emails to User B, and so on.

Any other approach to handle this?

Source: Laravel

Leave a Reply