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

Abnormal emails management

lotkfb49cb422588
diff --git a/core/api/abnormal_recipient/abnormal_recipient.go b/core/api/abnormal_recipient/abnormal_recipient.go
new file mode 100644
index 00000000..b42e6666
--- /dev/null
+++ b/core/api/abnormal_recipient/abnormal_recipient.go
@@ -0,0 +1,17 @@
+// =================================================================================
+// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
+// =================================================================================
+
+package abnormal_recipient
+
+import (
+ "context"
+
+ "billionmail-core/api/abnormal_recipient/v1"
+)
+
+type IAbnormalRecipientV1 interface {
+ ListAbnormalRecipient(ctx context.Context, req *v1.ListAbnormalRecipientReq) (res *v1.ListAbnormalRecipientRes, err error)
+ AddAbnormalRecipient(ctx context.Context, req *v1.AddAbnormalRecipientReq) (res *v1.AddAbnormalRecipientRes, err error)
+ DeleteAbnormalRecipient(ctx context.Context, req *v1.DeleteAbnormalRecipientReq) (res *v1.DeleteAbnormalRecipientRes, err error)
+}
diff --git a/core/api/abnormal_recipient/v1/abnormal_recipient.go b/core/api/abnormal_recipient/v1/abnormal_recipient.go
new file mode 100644
index 00000000..6b910b26
--- /dev/null
+++ b/core/api/abnormal_recipient/v1/abnormal_recipient.go
@@ -0,0 +1,53 @@
+package v1
+
+import (
+ "billionmail-core/utility/types/api_v1"
+ "github.com/gogf/gf/v2/frame/g"
+)
+
+type AbnormalRecipient struct {
+ Id int `json:"id" dc:"Group ID"`
+ Recipient string `json:"recipient" dc:"Recipient Email"`
+ CreateTime int `json:"create_time" dc:"Create Time"`
+ Description string `json:"description" dc:"Description"`
+ Count int `json:"count" dc:"Count"`
+ AddType int `json:"add_type" dc:"Add Type"`
+ // AddType:Description 1: Manually added, 2: Automatically scanned, 3: Manually scanned
+}
+
+type ListAbnormalRecipientReq struct {
+ g.Meta `path:"/abnormal_recipient/list" method:"get" tags:"Abnormal Recipient" summary:"List abnormal recipients"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ Page int `json:"page" v:"required|min:1" dc:"Page Number"`
+ PageSize int `json:"page_size" v:"required|min:1" dc:"Page Size"`
+ Keyword string `json:"keyword" dc:"Search Keyword"`
+ AddType int `json:"add_type" dc:"Add Type"`
+}
+
+type ListAbnormalRecipientRes struct {
+ api_v1.StandardRes
+ Data struct {
+ Total int `json:"total" dc:"Total Count"`
+ List []*AbnormalRecipient `json:"list" dc:"Abnormal Recipient List"`
+ } `json:"data"`
+}
+
+type AddAbnormalRecipientReq struct {
+ g.Meta `path:"/abnormal_recipient/add" method:"post" tags:"Abnormal Recipient" summary:"Add abnormal recipient"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ Recipient string `json:"recipient" v:"required|email" dc:"Recipient Email"`
+}
+
+type AddAbnormalRecipientRes struct {
+ api_v1.StandardRes
+}
+
+type DeleteAbnormalRecipientReq struct {
+ g.Meta `path:"/abnormal_recipient/delete" method:"post" tags:"Abnormal Recipient" summary:"Delete abnormal recipient"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ Id int `json:"id" v:"required|min:1" dc:"ID"`
+}
+
+type DeleteAbnormalRecipientRes struct {
+ api_v1.StandardRes
+}
diff --git a/core/internal/cmd/cmd.go b/core/internal/cmd/cmd.go
index 46bb34c3..02712d30 100644
--- a/core/internal/cmd/cmd.go
+++ b/core/internal/cmd/cmd.go
@@ -2,6 +2,7 @@ package cmd
import (
"billionmail-core/internal/consts"
+ "billionmail-core/internal/controller/abnormal_recipient"
"billionmail-core/internal/controller/batch_mail"
"billionmail-core/internal/controller/contact"
"billionmail-core/internal/controller/dockerapi"
@@ -148,6 +149,7 @@ var (
email_template.NewV1(),
batch_mail.NewV1(),
files.NewV1(),
+ abnormal_recipient.NewV1(),
)
})
diff --git a/core/internal/controller/abnormal_recipient/abnormal_recipient.go b/core/internal/controller/abnormal_recipient/abnormal_recipient.go
new file mode 100644
index 00000000..b299fc15
--- /dev/null
+++ b/core/internal/controller/abnormal_recipient/abnormal_recipient.go
@@ -0,0 +1,5 @@
+// =================================================================================
+// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
+// =================================================================================
+
+package abnormal_recipient
diff --git a/core/internal/controller/abnormal_recipient/abnormal_recipient_new.go b/core/internal/controller/abnormal_recipient/abnormal_recipient_new.go
new file mode 100644
index 00000000..382c235e
--- /dev/null
+++ b/core/internal/controller/abnormal_recipient/abnormal_recipient_new.go
@@ -0,0 +1,15 @@
+// =================================================================================
+// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
+// =================================================================================
+
+package abnormal_recipient
+
+import (
+ "billionmail-core/api/abnormal_recipient"
+)
+
+type ControllerV1 struct{}
+
+func NewV1() abnormal_recipient.IAbnormalRecipientV1 {
+ return &ControllerV1{}
+}
diff --git a/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_add_abnormal_recipient.go b/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_add_abnormal_recipient.go
new file mode 100644
index 00000000..3ced1692
--- /dev/null
+++ b/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_add_abnormal_recipient.go
@@ -0,0 +1,25 @@
+package abnormal_recipient
+
+import (
+ "billionmail-core/api/abnormal_recipient/v1"
+ "billionmail-core/internal/service/abnormal_recipient"
+ "billionmail-core/internal/service/public"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+ "strings"
+)
+
+func (c *ControllerV1) AddAbnormalRecipient(ctx context.Context, req *v1.AddAbnormalRecipientReq) (res *v1.AddAbnormalRecipientRes, err error) {
+ res = &v1.AddAbnormalRecipientRes{}
+
+ recipient := strings.TrimSpace(req.Recipient)
+
+ err = abnormal_recipient.Add(ctx, recipient)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to add exception recipient: {}", err.Error())))
+ return
+ }
+
+ res.SetSuccess(public.LangCtx(ctx, "The exception recipient was added successfully"))
+ return
+}
diff --git a/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_delete_abnormal_recipient.go b/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_delete_abnormal_recipient.go
new file mode 100644
index 00000000..e15a83f5
--- /dev/null
+++ b/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_delete_abnormal_recipient.go
@@ -0,0 +1,21 @@
+package abnormal_recipient
+
+import (
+ "billionmail-core/api/abnormal_recipient/v1"
+ "billionmail-core/internal/service/abnormal_recipient"
+ "billionmail-core/internal/service/public"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+)
+
+func (c *ControllerV1) DeleteAbnormalRecipient(ctx context.Context, req *v1.DeleteAbnormalRecipientReq) (res *v1.DeleteAbnormalRecipientRes, err error) {
+ res = &v1.DeleteAbnormalRecipientRes{}
+
+ err = abnormal_recipient.Delete(ctx, req.Id)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to remove exception recipient: {}", err.Error())))
+ return
+ }
+ res.SetSuccess(public.LangCtx(ctx, "The exception recipient was removed successfully"))
+ return
+}
diff --git a/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_list_abnormal_recipient.go b/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_list_abnormal_recipient.go
new file mode 100644
index 00000000..09e0f2db
--- /dev/null
+++ b/core/internal/controller/abnormal_recipient/abnormal_recipient_v1_list_abnormal_recipient.go
@@ -0,0 +1,37 @@
+package abnormal_recipient
+
+import (
+ "billionmail-core/api/abnormal_recipient/v1"
+ "billionmail-core/internal/service/abnormal_recipient"
+ "billionmail-core/internal/service/public"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+)
+
+func (c *ControllerV1) ListAbnormalRecipient(ctx context.Context, req *v1.ListAbnormalRecipientReq) (res *v1.ListAbnormalRecipientRes, err error) {
+ res = &v1.ListAbnormalRecipientRes{}
+
+ total, list, err := abnormal_recipient.GetListWithPage(ctx, req.Page, req.PageSize, req.Keyword, req.AddType)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to get the exception recipient list: {}", err.Error())))
+ return
+ }
+
+ apiList := make([]*v1.AbnormalRecipient, 0, len(list))
+ for _, item := range list {
+ apiList = append(apiList, &v1.AbnormalRecipient{
+ Id: item.Id,
+ Recipient: item.Recipient,
+ CreateTime: item.CreateTime,
+ Description: item.Description,
+ Count: item.Count,
+ AddType: item.AddType,
+ })
+ }
+
+ res.Data.Total = total
+ res.Data.List = apiList
+
+ res.SetSuccess(public.LangCtx(ctx, "The list of exception recipients was successfully obtained"))
+ return
+}
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go b/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go
index 2f567c94..4ee0d987 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go
@@ -2,7 +2,6 @@ package batch_mail
import (
"billionmail-core/internal/service/batch_mail"
- "billionmail-core/internal/service/contact"
"billionmail-core/internal/service/email_template"
"billionmail-core/internal/service/public"
"context"
@@ -56,12 +55,8 @@ func (c *ControllerV1) ListTasks(ctx context.Context, req *v1.ListTasksReq) (res
// Fix: Check if recipient_count is valid
if task.RecipientCount <= 0 {
- groupIdsInt := make([]int, 0, len(groupIds))
- for _, groupId := range groupIds {
- groupIdsInt = append(groupIdsInt, gconv.Int(groupId))
- }
- actualCount, err := contact.GetGroupContactCount(ctx, groupIdsInt)
+ actualCount, err := batch_mail.GetActualRecipientCount(ctx, task.Id)
if err == nil && actualCount > 0 {
// Update the recipient count in the database
_ = batch_mail.UpdateRecipientCount(ctx, task.Id, actualCount)
diff --git a/core/internal/model/entity/batch_mail.go b/core/internal/model/entity/batch_mail.go
index 1c800dba..03f5e0a0 100644
--- a/core/internal/model/entity/batch_mail.go
+++ b/core/internal/model/entity/batch_mail.go
@@ -64,3 +64,13 @@ type RecipientInfo struct {
MessageId string `json:"message_id" dc:"Email Message-ID"`
CreateTime int `json:"create_time" dc:"Create Time"`
}
+
+type AbnormalRecipient struct {
+ Id int `json:"id" dc:"Group ID"`
+ Recipient string `json:"recipient" dc:"Recipient Email"`
+ CreateTime int `json:"create_time" dc:"Create Time"`
+ Description string `json:"description" dc:"Description"`
+ Count int `json:"count" dc:"Count"`
+ AddType int `json:"add_type" dc:"Add Type"`
+ // AddType 1: Manually added, 2: Automatically scanned, 3: Manually scanned
+}
diff --git a/core/internal/service/abnormal_recipient/abnormal_recipient.go b/core/internal/service/abnormal_recipient/abnormal_recipient.go
new file mode 100644
index 00000000..0a83ffa7
--- /dev/null
+++ b/core/internal/service/abnormal_recipient/abnormal_recipient.go
@@ -0,0 +1,77 @@
+package abnormal_recipient
+
+import (
+ "billionmail-core/internal/model/entity"
+ "context"
+ "fmt"
+ "github.com/gogf/gf/v2/frame/g"
+ "time"
+)
+
+func GetListWithPage(ctx context.Context, page, pageSize int, keyword string, addType int) (total int, list []*entity.AbnormalRecipient, err error) {
+
+ if page <= 0 {
+ page = 1
+ }
+ if pageSize <= 0 {
+ pageSize = 10
+ }
+
+ model := g.DB().Model("abnormal_recipient").Safe()
+
+ if keyword != "" {
+ model = model.WhereLike("recipient", "%"+keyword+"%")
+ }
+ if addType > 0 {
+ model = model.Where("add_type", addType)
+ }
+
+ total, err = model.Count()
+ if err != nil {
+ return 0, nil, fmt.Errorf("Failed to get the total number of exception recipients: %w", err)
+ }
+
+ list = make([]*entity.AbnormalRecipient, 0)
+ err = model.Page(page, pageSize).
+ Order("create_time DESC").
+ Scan(&list)
+
+ if err != nil {
+ return 0, nil, fmt.Errorf("Failed to get the exception recipient list: %w", err)
+ }
+
+ return total, list, nil
+}
+
+func Add(ctx context.Context, recipient string) error {
+
+ now := time.Now().Unix()
+ _, err := g.DB().Model("abnormal_recipient").
+ Data(g.Map{
+ "recipient": recipient,
+ "count": 3,
+ "add_type": 1,
+ "description": "Manually added",
+ "create_time": now,
+ }).
+ InsertIgnore()
+
+ if err != nil {
+ return fmt.Errorf("Failed to add exception recipient: %w", err)
+ }
+
+ return nil
+}
+
+func Delete(ctx context.Context, id int) error {
+
+ _, err := g.DB().Model("abnormal_recipient").
+ Where("id", id).
+ Delete()
+
+ if err != nil {
+ return fmt.Errorf("Failed to remove exception recipient: %w", err)
+ }
+
+ return nil
+}
diff --git a/core/internal/service/batch_mail/batch_mail.go b/core/internal/service/batch_mail/batch_mail.go
index 6fa8ac8d..67c35bb2 100644
--- a/core/internal/service/batch_mail/batch_mail.go
+++ b/core/internal/service/batch_mail/batch_mail.go
@@ -247,8 +247,31 @@ func CreateTaskWithRecipients(ctx context.Context, req *v1.CreateTaskReq, addTyp
return gerror.New(public.LangCtx(ctx, "Failed to create task {}", err.Error()))
}
+ var abnormalRecipients []struct {
+ Recipient string `json:"recipient"`
+ Count int `json:"count"`
+ }
+
+ err = tx.Model("abnormal_recipient").
+ Where("count >= ?", 3).
+ Fields("recipient, count").
+ Scan(&abnormalRecipients)
+
+ if err != nil {
+ g.Log().Warning(ctx, "Failed to get the exception recipient list: %v", err)
+ }
+
+ abnormalMap := make(map[string]int)
+ if len(abnormalRecipients) > 0 {
+ for _, ar := range abnormalRecipients {
+ abnormalMap[ar.Recipient] = ar.Count
+ }
+ }
+
// import recipient information
totalRecipients := 0
+ totalSkipped := 0
+
for _, groupId := range req.GroupIds {
contacts, err := GetActiveContacts(ctx, groupId)
if err != nil {
@@ -257,16 +280,34 @@ func CreateTaskWithRecipients(ctx context.Context, req *v1.CreateTaskReq, addTyp
if len(contacts) == 0 {
continue
}
- err = ImportRecipients(ctx, taskId, contacts)
+
+ // abnormal skip
+ filteredContacts := make([]*entity.Contact, 0, len(contacts))
+ skippedInGroup := 0
+
+ for _, contact := range contacts {
+ if _, exists := abnormalMap[contact.Email]; exists {
+ skippedInGroup++
+ continue
+ }
+ filteredContacts = append(filteredContacts, contact)
+ }
+
+ if skippedInGroup > 0 {
+ totalSkipped += skippedInGroup
+
+ }
+
+ if len(filteredContacts) == 0 {
+ continue
+ }
+
+ err = ImportRecipients(ctx, taskId, filteredContacts)
if err != nil {
return gerror.New(public.LangCtx(ctx, "Failed to import recipients for group {}: {}", groupId, err.Error()))
}
- totalRecipients += len(contacts)
- }
-
- if totalRecipients == 0 {
- return gerror.New("No recipients found in the selected groups")
+ totalRecipients += len(filteredContacts)
}
// update recipient count
@@ -368,3 +409,16 @@ func GetTaskSendingStats(ctx context.Context, taskID int) (int, int, error) {
// fmt.Println("success count:", successCount, "failed count:", failedCount)
return successCount, failedCount, nil
}
+
+func GetActualRecipientCount(ctx context.Context, taskId int) (int, error) {
+
+ count, err := g.DB().Model("recipient_info").
+ Where("task_id", taskId).
+ Count()
+
+ if err != nil {
+ return 0, fmt.Errorf("failed to get recipient count: %w", err)
+ }
+
+ return count, nil
+}
diff --git a/core/internal/service/database_initialization/batch_mail.go b/core/internal/service/database_initialization/batch_mail.go
index 8e6ecc2c..e1b1cdf1 100644
--- a/core/internal/service/database_initialization/batch_mail.go
+++ b/core/internal/service/database_initialization/batch_mail.go
@@ -89,10 +89,21 @@ func init() {
FOREIGN KEY (task_id) REFERENCES email_tasks(id) ON DELETE SET NULL
)`,
+ `CREATE TABLE IF NOT EXISTS abnormal_recipient (
+ id SERIAL PRIMARY KEY,
+ create_time INTEGER NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
+ recipient VARCHAR(320) NOT NULL,
+ count INTEGER NOT NULL,
+ description VARCHAR(255) NOT NULL,
+ add_type SMALLINT NOT NULL DEFAULT 0,
+ UNIQUE(recipient)
+ )`,
+
`CREATE INDEX IF NOT EXISTS idx_unsubscribe_email ON unsubscribe_records (email)`,
`CREATE INDEX IF NOT EXISTS idx_unsubscribe_time ON unsubscribe_records (unsubscribe_time)`,
`CREATE INDEX IF NOT EXISTS idx_bm_contacts_email ON bm_contacts (email)`,
`CREATE INDEX IF NOT EXISTS idx_bm_contacts_active ON bm_contacts (active)`,
+ `CREATE INDEX IF NOT EXISTS idx_abnormal_recipient_count ON abnormal_recipient (recipient,count)`,
}
for _, sql := range batchMailSQLList {