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
Add the default domain to the sender domain
lotkc7f5b9085938
diff --git a/core/api/domains/domains.go b/core/api/domains/domains.goindex 094ab13f..327dcb9a 100644--- a/core/api/domains/domains.go+++ b/core/api/domains/domains.go@@ -19,6 +19,7 @@ type IDomainsV1 interface {FreshDNSRecords(ctx context.Context, req *v1.FreshDNSRecordsReq) (res *v1.FreshDNSRecordsRes, err error)SetSSL(ctx context.Context, req *v1.SetSSLReq) (res *v1.SetSSLRes, err error)GetSSL(ctx context.Context, req *v1.GetSSLReq) (res *v1.GetSSLRes, err error)+ SetDefaultDomain(ctx context.Context, req *v1.SetDefaultDomainReq) (res *v1.SetDefaultDomainRes, err error)ApplyCert(ctx context.Context, req *v1.ApplyCertReq) (res *v1.ApplyCertRes, err error)GetCertList(ctx context.Context, req *v1.GetCertListReq) (res *v1.GetCertListRes, err error)ConsoleApplyCert(ctx context.Context, req *v1.ConsoleApplyCertReq) (res *v1.ConsoleApplyCertRes, err error)diff --git a/core/api/domains/v1/domains.go b/core/api/domains/v1/domains.goindex 4ead157c..53d488e9 100644--- a/core/api/domains/v1/domains.go+++ b/core/api/domains/v1/domains.go@@ -46,6 +46,7 @@ type Domain struct {DNSRecords DNSRecords `json:"dns_records" dc:"DNS records"`CertInfo CertInfo `json:"cert_info" dc:"Certificate information"`Catchall string `json:"email" dc:"Cache all DNS records, used for domain verification"`+ Default int `json:"default" dc:"Default sender domain, 1-yes, 0-no"`}type AddDomainReq struct {@@ -148,3 +149,12 @@ type GetSSLRes struct {api_v1.StandardResData CertInfo `json:"data" dc:"Certificate information"`}++type SetDefaultDomainReq struct {+ g.Meta `path:"/domains/set_default_domain" tags:"Domain" method:"post" sm:"Set default sender domain" in:"body"`+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`+ Domain string `json:"domain" v:"required|domain" dc:"Domain"`+}+type SetDefaultDomainRes struct {+ api_v1.StandardRes+}diff --git a/core/internal/controller/domains/domains_v1_set_default_domain.go b/core/internal/controller/domains/domains_v1_set_default_domain.gonew file mode 100644index 00000000..47b9f4e6--- /dev/null+++ b/core/internal/controller/domains/domains_v1_set_default_domain.go@@ -0,0 +1,50 @@+package domains++import (+ "billionmail-core/internal/service/public"+ "context"+ "github.com/gogf/gf/v2/frame/g"++ "github.com/gogf/gf/v2/errors/gcode"+ "github.com/gogf/gf/v2/errors/gerror"++ "billionmail-core/api/domains/v1"+)++func (c *ControllerV1) SetDefaultDomain(ctx context.Context, req *v1.SetDefaultDomainReq) (res *v1.SetDefaultDomainRes, err error) {+ res = &v1.SetDefaultDomainRes{}++ // 1. Validate if the domain exists and is active+ var count int+ count, err = g.DB().Model("domain").+ Where("domain", req.Domain).+ Where("active", 1).+ Count()++ if err != nil {+ res.SetError(gerror.New(public.LangCtx(ctx, "check domain failed: {}", err.Error())))+ return res, nil+ }++ if count == 0 {+ res.SetError(gerror.NewCode(gcode.CodeNotFound, "Domain does not exist or is not activated"))+ return res, nil+ }++ // 2. Write to bm_options+ _, err = g.DB().Model("bm_options").+ Data(g.Map{+ "name": "default_sender_domain",+ "value": req.Domain,+ }).+ OnConflict("name").+ Save()++ if err != nil {+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to set default sender domain: {}", err.Error())))+ return+ }++ res.SetSuccess(public.LangCtx(ctx, "Default sender domain set successfully"))+ return+}diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.goindex 2dad6db0..7a9e339e 100644--- a/core/internal/service/domains/domains.go+++ b/core/internal/service/domains/domains.go@@ -183,6 +183,12 @@ func Get(ctx context.Context, keyword string, page, pageSize int) ([]v1.Domain,return nil, 0, err}+ var defaultDomain string+ val, err := g.DB().Model("bm_options").Where("name", "default_sender_domain").Value("value")+ if err == nil && val != nil {+ defaultDomain = val.String()+ }+crt := mail_service.NewCertificate()defer crt.Close()@@ -190,6 +196,12 @@ func Get(ctx context.Context, keyword string, page, pageSize int) ([]v1.Domain,wg := sync.WaitGroup{}for i, domain := range domains {+ if domain.Domain == defaultDomain {+ domains[i].Default = 1+ } else {+ domains[i].Default = 0+ }+wg.Add(1)go func(i int, domain v1.Domain) {defer wg.Done()