Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Optimize the api for sending emails

lotkd8cc588d1b39
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
index 6e4d30ee..5f36cb70 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
@@ -39,16 +39,12 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplate
query = query.LeftJoin("mailstat_message_ids mi", "aml.message_id=mi.message_id")
query = query.LeftJoin("mailstat_send_mails sm", "mi.postfix_message_id=sm.postfix_message_id")
- query = query.LeftJoin("mailstat_opened o", "sm.postfix_message_id=o.postfix_message_id")
- query = query.LeftJoin("mailstat_clicked c", "sm.postfix_message_id=c.postfix_message_id")
query = query.Where("aml.api_id", item.Id)
// 统计各项数据
query.Fields(
"count(*) as sends",
"coalesce(sum(case when sm.status='sent' and sm.dsn like '2.%' then 1 else 0 end), 0) as delivered",
- "count(distinct o.postfix_message_id) as opened",
- "count(distinct c.postfix_message_id) as clicked",
"coalesce(sum(case when sm.status='bounced' then 1 else 0 end), 0) as bounced",
)
@@ -63,25 +59,35 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplate
item.SuccessCount = result["delivered"].Int()
item.FailCount = result["bounced"].Int()
+ // 3. 统计打开、点击(直接用campaign_id)
+ apiCampaignId := item.Id + 1000000000
+ openedCount, _ := g.DB().Model("mailstat_opened").
+ Where("campaign_id", apiCampaignId).
+ Fields("count(distinct postfix_message_id) as opened").
+ Value()
+ clickedCount, _ := g.DB().Model("mailstat_clicked").
+ Where("campaign_id", apiCampaignId).
+ Fields("count(distinct postfix_message_id) as clicked").
+ Value()
+
if item.SendCount > 0 {
// 送达率 = 成功送达数/总发送数
item.DeliveryRate = public.Round(float64(item.SuccessCount)/float64(item.SendCount)*100, 2)
// 退信率 = 退信数/总发送数
item.BounceRate = public.Round(float64(item.FailCount)/float64(item.SendCount)*100, 2)
- // 打开率 = 打开数/总发送数
- item.OpenRate = public.Round(float64(result["opened"].Int())/float64(item.SendCount)*100, 2)
- // 点击率 = 点击数/总发送数
- item.ClickRate = public.Round(float64(result["clicked"].Int())/float64(item.SendCount)*100, 2)
+ item.OpenRate = public.Round(float64(openedCount.Int())/float64(item.SendCount)*100, 2)
+ item.ClickRate = public.Round(float64(clickedCount.Int())/float64(item.SendCount)*100, 2)
} else {
item.DeliveryRate = 0
item.BounceRate = 0
item.OpenRate = 0
item.ClickRate = 0
}
- }
+ }
res.Data.Total = total
res.Data.List = list
res.SetSuccess("获取API列表成功")
return res, nil
+
}