发送每日摘要电子邮件

时间:2020-09-29 23:29:05

标签: php laravel

我正在为日常电子邮件通知编写一份cron作业。

这是可以说的情况

  1. 用户A 每天获得10个潜在客户。
  2. 用户B 每天获得5个潜在客户。
  3. 用户C 每天获得2个潜在客户。

我想向其中包含10个潜在客户信息的用户A发送一封电子邮件

然后给用户B一封电子邮件,其中包含5个潜在客户信息。

然后给用户C一封电子邮件,其中包含2个潜在客户信息。

所以我想尝试创建一个摘要邮件,其中包含特定用户的潜在客户信息。

   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));

        }

如果我在foreach循环中编写代码,那么最终我会向用户A发送10封电子邮件,向用户B发送5封电子邮件,依此类推。

还有其他处理方法吗?

2 个答案:

答案 0 :(得分:1)

您可以首先根据user_email对结果进行分组,然后在第二个循环中发送电子邮件。

$grouped_data = [];

foreach ($today_leads as $lead) {
     $grouped_data[$lead->user_email][] = [
                'user_id' => $lead->user_id,
                'user_fullname' => $lead->user_fullname,
                'user_email' => $lead->user_email,   // you can remove it if not required in email body anywhere
                'lead_firstname' => $lead->first_name,
                'lead_lastname' => $lead->last_name,
                'lead_email' => $lead->email,
                'lead_phone' => $lead->phone,
                'lead_source' => $lead->source,
            ];
}

foreach($grouped_data AS $user_email => $data)
{
    Mail::to($user_email)->send(new DailyLeadSummary($data));
}

答案 1 :(得分:0)

看您的实现,您走在正确的道路上,但是您需要进行一些调整。

首先,您需要查询用户表以在特定日期获得潜在客户。这样做


// This will return only users who got leads today
$usersWithLeads = User::whereHas('leads', function($query) {
     // This will match all leads that was created today
     $query->where('created_at', >=, now()->toStartOfDay());
})->get();


// then you can loop over $usersWithLeads

foreach($usersWithLeads as $usersWithLead) {
     // You can then retrieve the leads gotten today via the leads relationship 
    $leads = $usersWithLead->leads()->where('created_at', now()->startOfDay());

    // Then send your mail
    Mail::to($usersWithLead)->send(new DailyLeadSummary($leads));
}

然后可以根据需要在您的可邮寄视图上呈现销售线索。但这可以确保每个用户仅发送一封电子邮件。

此外,您可以修改查询以检索$ usersWithLeads来检索每日潜在客户,如下所示:

$usersWithLeads = User::with(['leads' => function ($query) {
    $query->where('created_at', now()->toStartOfDay());
}])->whereHas('leads', function($query) {
     // This will match all leads that was created today
     $query->where('created_at', >=, now()->toStartOfDay());
})->get();

然后您的循环将如下所示:

foreach($usersWithLeads as $usersWithLead) {
    // Then send your mail
    Mail::to($usersWithLead)->send(new DailyLeadSummary($usersWithLead->leads));
}

注意::查询中使用的now()方法是Carbon的{​​{1}}帮助方法,它为您提供了当前时间,并将{{1 Carbon::now()上的}}方法检索日期时间为0:00(开始日期)

此答案还假设您有一个表来保存->startOfDay(),并在您的now()模型中声明了一个关系,例如:

leads

如果您需要更多帮助,请告诉我。