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
feat: add group_id field to API templates and implement contact grouping logic
lotkfb94ed444623
diff --git a/core/api/batch_mail/v1/api_mail.go b/core/api/batch_mail/v1/api_mail.goindex 3d737b64..8e30d3da 100644--- a/core/api/batch_mail/v1/api_mail.go+++ b/core/api/batch_mail/v1/api_mail.go@@ -11,6 +11,7 @@ type ApiTemplates struct {ApiKey string `json:"api_key" dc:"api key"`ApiName string `json:"api_name" dc:"api name"`TemplateId int `json:"template_id" dc:"template id"`+ GroupId int `json:"group_id" dc:"group id"`Subject string `json:"subject" dc:"subject"`Addresser string `json:"addresser" dc:"addresser"`FullName string `json:"full_name" dc:"full name"`@@ -46,6 +47,7 @@ type ApiTemplatesListReq struct {type ApiTemplatesInfo struct {ApiTemplates+ GroupId int `json:"group_id" dc:"group id"`SendCount int `json:"send_count" dc:"send count"`SuccessCount int `json:"success_count" dc:"success count"`FailCount int `json:"fail_count" dc:"fail count"`@@ -93,6 +95,7 @@ type ApiTemplatesCreateReq struct {Authorization string `json:"authorization" dc:"Authorization" in:"header"`ApiName string `json:"api_name" dc:"api name"`TemplateId int `json:"template_id" dc:"template id"`+ GroupId int `json:"group_id" dc:"Associated group ID"`Subject string `json:"subject" dc:"subject"`Addresser string `json:"addresser" dc:"addresser"`FullName string `json:"full_name" dc:"full name"`@@ -115,6 +118,7 @@ type ApiTemplatesUpdateReq struct {ID int `json:"id" dc:"id"`ApiName string `json:"api_name" dc:"api name"`TemplateId int `json:"template_id" dc:"template id"`+ GroupId int `json:"group_id" dc:"Associated group ID"`Subject string `json:"subject" dc:"subject"`Addresser string `json:"addresser" dc:"addresser"`FullName string `json:"full_name" dc:"full name"`diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_mail_batch_send.go b/core/internal/controller/batch_mail/batch_mail_v1_api_mail_batch_send.goindex 09a3c05e..50ec49dc 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_api_mail_batch_send.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_mail_batch_send.go@@ -1,14 +1,16 @@package batch_mailimport (+ "billionmail-core/internal/service/contact""billionmail-core/internal/service/mail_service""billionmail-core/internal/service/public""context"- "github.com/gogf/gf/v2/errors/gerror"- "github.com/gogf/gf/v2/frame/g""strings""time"+ "github.com/gogf/gf/v2/errors/gerror"+ "github.com/gogf/gf/v2/frame/g"+"billionmail-core/api/batch_mail/v1")@@ -83,10 +85,20 @@ func (c *ControllerV1) ApiMailBatchSend(ctx context.Context, req *v1.ApiMailBatcnow := int(time.Now().Unix())for _, recipient := range validRecipients {- _, err = ensureContactAndGroup(ctx, recipient, apiTemplate.Id)- if err != nil {-- continue+ if apiTemplate.GroupId > 0 {+ // Add to the specified existing group+ _, err = contact.AddContactToGroup(ctx, recipient, apiTemplate.GroupId)+ if err != nil {+ g.Log().Warningf(ctx, "Failed to add contact %s to group %d: %v", recipient, apiTemplate.GroupId, err)+ continue+ }+ } else {+ // Use the old logic to create an API-specific group+ _, err = ensureContactAndGroup(ctx, recipient, apiTemplate.Id)+ if err != nil {+ g.Log().Warningf(ctx, "Failed to ensure contact and group for %s with API ID %d: %v", recipient, apiTemplate.Id, err)+ continue+ }}// 生成messageIdsender, err := mail_service.NewEmailSenderWithLocal(addresser)diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go b/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.goindex 714601af..43c0d45e 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go@@ -3,16 +3,18 @@ package batch_mailimport ("billionmail-core/api/batch_mail/v1""billionmail-core/internal/model/entity"+ "billionmail-core/internal/service/contact""billionmail-core/internal/service/mail_service""billionmail-core/internal/service/public""context""database/sql""errors""fmt"- "github.com/gogf/gf/v2/errors/gerror"- "github.com/gogf/gf/v2/frame/g""strings""time"++ "github.com/gogf/gf/v2/errors/gerror"+ "github.com/gogf/gf/v2/frame/g")func (c *ControllerV1) ApiMailSend(ctx context.Context, req *v1.ApiMailSendReq) (res *v1.ApiMailSendRes, err error) {@@ -61,11 +63,24 @@ func (c *ControllerV1) ApiMailSend(ctx context.Context, req *v1.ApiMailSendReq)}// 4. process contact and group- _, err = ensureContactAndGroup(ctx, req.Recipient, apiTemplate.Id)- if err != nil {- res.Code = 1003- res.SetError(gerror.New(public.LangCtx(ctx, "Failed to process recipient: {}", err.Error())))- return res, nil+ if apiTemplate.GroupId > 0 {+ // Add to the specified existing group+ _, err = contact.AddContactToGroup(ctx, req.Recipient, apiTemplate.GroupId)+ if err != nil {+ g.Log().Warningf(ctx, "Failed to add contact %s to group %d: %v", req.Recipient, apiTemplate.GroupId, err)+ res.Code = 1003+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to process recipient: {}", err.Error())))+ return res, nil+ }+ } else {+ // Use the old logic to create an API-specific group+ _, err = ensureContactAndGroup(ctx, req.Recipient, apiTemplate.Id)+ if err != nil {+ g.Log().Warningf(ctx, "Failed to ensure contact and group for %s with API ID %d: %v", req.Recipient, apiTemplate.Id, err)+ res.Code = 1003+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to process recipient: {}", err.Error())))+ return res, nil+ }}// 5. process addresserdiff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_create.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_create.goindex 6afc9dde..7d60cd57 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_create.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_create.go@@ -56,6 +56,7 @@ func (c *ControllerV1) ApiTemplatesCreate(ctx context.Context, req *v1.ApiTempla"api_key": apiKey,"api_name": req.ApiName,"template_id": req.TemplateId,+ "group_id": req.GroupId,"subject": req.Subject,"addresser": req.Addresser,"full_name": req.FullName,diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.goindex 37071d3a..7b7251df 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go@@ -5,11 +5,12 @@ import ("billionmail-core/internal/consts""billionmail-core/internal/service/public""context"- "github.com/gogf/gf/v2/errors/gerror"- "github.com/gogf/gf/v2/frame/g""net""strings""time"++ "github.com/gogf/gf/v2/errors/gerror"+ "github.com/gogf/gf/v2/frame/g")func (c *ControllerV1) ApiTemplatesUpdate(ctx context.Context, req *v1.ApiTemplatesUpdateReq) (res *v1.ApiTemplatesUpdateRes, err error) {@@ -59,6 +60,7 @@ func (c *ControllerV1) ApiTemplatesUpdate(ctx context.Context, req *v1.ApiTempla"active": req.Active,"expire_time": req.ExpireTime,"update_time": now,+ "group_id": req.GroupId,}_, err = tx.Model("api_templates").diff --git a/core/internal/model/entity/batch_mail.go b/core/internal/model/entity/batch_mail.goindex 0e229307..3db96eaa 100644--- a/core/internal/model/entity/batch_mail.go+++ b/core/internal/model/entity/batch_mail.go@@ -156,6 +156,7 @@ type ApiTemplates struct {ApiKey string `json:"api_key" dc:"api key"`ApiName string `json:"api_name" dc:"api name"`TemplateId int `json:"template_id" dc:"template id"`+ GroupId int `json:"group_id" dc:"group id"`Subject string `json:"subject" dc:"subject"`Addresser string `json:"addresser" dc:"addresser"`FullName string `json:"full_name" dc:"full name"`diff --git a/core/internal/service/contact/contact.go b/core/internal/service/contact/contact.goindex 5844161b..155d259e 100644--- a/core/internal/service/contact/contact.go+++ b/core/internal/service/contact/contact.go@@ -7,12 +7,13 @@ import ("database/sql""encoding/json""fmt"- "github.com/gogf/gf/crypto/gmd5"- "github.com/gogf/gf/v2/database/gdb"- "github.com/gogf/gf/v2/frame/g""math/rand""strings""time"++ "github.com/gogf/gf/crypto/gmd5"+ "github.com/gogf/gf/v2/database/gdb"+ "github.com/gogf/gf/v2/frame/g")func CreateGroup(ctx context.Context, name, description string, double_optin int) (int, error) {@@ -542,3 +543,43 @@ func GfMd5Short() string {str := fmt.Sprintf("%s_%d_%d", time.Now().UnixNano(), rand.Intn(100000))return gmd5.MustEncryptString(str)[:12]}++// AddContactToGroup adds a contact to a specified group if it doesn't already exist.+func AddContactToGroup(ctx context.Context, email string, groupId int) (*entity.Contact, error) {+ // Check if the contact already exists in the group+ var existingContact entity.Contact+ err := g.DB().Model("bm_contacts").Ctx(ctx).Where("email", email).Where("group_id", groupId).Scan(&existingContact)+ if err != nil && err != sql.ErrNoRows {+ g.Log().Errorf(ctx, "Failed to check for existing contact %s in group %d: %v", email, groupId, err)+ return nil, err+ }++ // If contact already exists, return it+ if existingContact.Id > 0 {+ return &existingContact, nil+ }++ // If contact does not exist, create a new one+ newContact := &entity.Contact{+ Email: email,+ GroupId: groupId,+ Active: 1,+ Status: 1,+ CreateTime: int(time.Now().Unix()),+ }++ result, err := g.DB().Model("bm_contacts").Ctx(ctx).Data(newContact).FieldsEx("id").Insert()+ if err != nil {+ g.Log().Errorf(ctx, "Failed to insert new contact %s into group %d: %v", email, groupId, err)+ return nil, err+ }++ newId, err := result.LastInsertId()+ if err != nil {+ g.Log().Errorf(ctx, "Failed to get last insert ID for contact %s: %v", email, err)+ return nil, err+ }+ newContact.Id = int(newId)++ return newContact, nil+}diff --git a/core/internal/service/database_initialization/batch_mail.go b/core/internal/service/database_initialization/batch_mail.goindex 21c6db89..4c158c35 100644--- a/core/internal/service/database_initialization/batch_mail.go+++ b/core/internal/service/database_initialization/batch_mail.go@@ -2,6 +2,7 @@ package database_initializationimport ("context"+"github.com/gogf/gf/v2/frame/g")@@ -257,6 +258,9 @@ func init() {_ = AddColumnIfNotExists("email_tasks", "tag_ids", "TEXT", "''", false)_ = AddColumnIfNotExists("email_tasks", "tag_logic", "VARCHAR(10)", "'AND'", false)+ //api_templates+ _ = AddColumnIfNotExists("api_templates", "group_id", "INTEGER", "0", true)+// unsubscribe_records_ = DropForeignKeyIfExists("unsubscribe_records", "group_id")_ = DropForeignKeyIfExists("unsubscribe_records", "template_id")