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

Batch set email queue configuration items

lotkad3c432bf356
diff --git a/core/api/mail_services/mail_services.go b/core/api/mail_services/mail_services.go
index 81203751..5abad7c4 100644
--- a/core/api/mail_services/mail_services.go
+++ b/core/api/mail_services/mail_services.go
@@ -28,5 +28,6 @@ type IMailServicesV1 interface {
FlushPostfixQueue(ctx context.Context, req *v1.FlushPostfixQueueReq) (res *v1.FlushPostfixQueueRes, err error)
FlushPostfixQueueById(ctx context.Context, req *v1.FlushPostfixQueueByIdReq) (res *v1.FlushPostfixQueueByIdRes, err error)
SetPostfixConfig(ctx context.Context, req *v1.SetPostfixConfigReq) (res *v1.SetPostfixConfigRes, err error)
+ SetAllPostfixConfig(ctx context.Context, req *v1.SetAllPostfixConfigReq) (res *v1.SetAllPostfixConfigRes, err error)
GetPostfixConfig(ctx context.Context, req *v1.GetPostfixConfigReq) (res *v1.GetPostfixConfigRes, err error)
}
diff --git a/core/api/mail_services/v1/postfix_queue.go b/core/api/mail_services/v1/postfix_queue.go
index b46d602e..68dfa35f 100644
--- a/core/api/mail_services/v1/postfix_queue.go
+++ b/core/api/mail_services/v1/postfix_queue.go
@@ -96,6 +96,21 @@ type SetPostfixConfigRes struct {
api_v1.StandardRes
}
+type SetAllPostfixConfigReq struct {
+ g.Meta `path:"/postfix_queue/set_all_config" method:"post" summary:"批量设置Postfix队列相关配置项"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ BounceQueueLifetime string `json:"bounce_queue_lifetime" dc:"Bounce queue lifetime"`
+ MaximalBackoffTime string `json:"maximal_backoff_time" dc:"Maximal backoff time"`
+ MaximalQueueLifetime string `json:"maximal_queue_lifetime" dc:"Maximal queue lifetime"`
+ MinimalBackoffTime string `json:"minimal_backoff_time" dc:"Minimal backoff time"`
+ QueueRunDelay string `json:"queue_run_delay" dc:"Queue run delay"`
+ TriggerTimeout string `json:"trigger_timeout" dc:"Trigger timeout"`
+}
+
+type SetAllPostfixConfigRes struct {
+ api_v1.StandardRes
+}
+
type GetPostfixConfigReq struct {
g.Meta `path:"/postfix_queue/get_config" method:"get" summary:"Get Postfix queue-related configuration items"`
Authorization string `json:"authorization" dc:"Authorization" in:"header"`
diff --git a/core/internal/controller/mail_services/mail_services_v1_set_all_postfix_config.go b/core/internal/controller/mail_services/mail_services_v1_set_all_postfix_config.go
new file mode 100644
index 00000000..bcf20542
--- /dev/null
+++ b/core/internal/controller/mail_services/mail_services_v1_set_all_postfix_config.go
@@ -0,0 +1,122 @@
+package mail_services
+
+import (
+ "billionmail-core/api/mail_services/v1"
+ "billionmail-core/internal/consts"
+ docker "billionmail-core/internal/service/dockerapi"
+ "billionmail-core/internal/service/public"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+ "github.com/gogf/gf/v2/frame/g"
+ "github.com/gogf/gf/v2/os/gmutex"
+ "io/ioutil"
+ "reflect"
+ "strings"
+)
+
+var setAllPostfixConfigLocker = gmutex.New()
+
+func (c *ControllerV1) SetAllPostfixConfig(ctx context.Context, req *v1.SetAllPostfixConfigReq) (res *v1.SetAllPostfixConfigRes, err error) {
+ res = &v1.SetAllPostfixConfigRes{}
+
+ fieldToKey := map[string]string{
+ "BounceQueueLifetime": "bounce_queue_lifetime",
+ "MaximalBackoffTime": "maximal_backoff_time",
+ "MaximalQueueLifetime": "maximal_queue_lifetime",
+ "MinimalBackoffTime": "minimal_backoff_time",
+ "QueueRunDelay": "queue_run_delay",
+ "TriggerTimeout": "trigger_timeout",
+ }
+
+ updated := make([]string, 0)
+ skipped := make([]string, 0)
+
+ validConfigs := make(map[string]string)
+
+ v := reflect.ValueOf(req).Elem()
+ for field, key := range fieldToKey {
+ val := v.FieldByName(field).String()
+ if strings.TrimSpace(val) == "" {
+ skipped = append(skipped, key)
+ continue
+ }
+ validConfigs[key] = strings.TrimSpace(val)
+ }
+
+ if len(validConfigs) == 0 {
+
+ res.SetSuccess(public.LangCtx(ctx, "No valid configurations to update"))
+ return res, nil
+ }
+
+ setAllPostfixConfigLocker.Lock()
+ defer setAllPostfixConfigLocker.Unlock()
+
+ postfixCfgPath := v1.ServiceType_Postfix
+
+ data, err := ioutil.ReadFile(postfixCfgPath)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "open main.cf failed: %v", err.Error())))
+ return res, nil
+ }
+
+ lines := strings.Split(string(data), "\n")
+ existingKeys := make(map[string]int) // key -> line index
+
+ for i, line := range lines {
+ trim := strings.TrimSpace(line)
+ if trim == "" || strings.HasPrefix(trim, "#") {
+ continue
+ }
+ parts := strings.SplitN(trim, "=", 2)
+ if len(parts) < 2 {
+ continue
+ }
+ key := strings.TrimSpace(parts[0])
+ existingKeys[key] = i
+ }
+
+ for key, value := range validConfigs {
+ if lineIndex, exists := existingKeys[key]; exists {
+ lines[lineIndex] = key + " = " + value
+ } else {
+ if len(lines) > 0 && lines[len(lines)-1] != "" {
+ lines = append(lines, "")
+ }
+ lines = append(lines, key+" = "+value)
+ }
+ updated = append(updated, key)
+ }
+
+ err = ioutil.WriteFile(postfixCfgPath, []byte(strings.Join(lines, "\n")), 0644)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "write main.cf failed: %v", err.Error())))
+ return res, nil
+ }
+
+ dk, err := docker.NewDockerAPI()
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "failed to create Docker API instance: %v", err.Error())))
+ return res, nil
+ }
+ defer dk.Close()
+
+ result, err := dk.ExecCommandByName(ctx, consts.SERVICES.Postfix, []string{"postfix", "reload"}, "root")
+ if err != nil || result == nil || result.ExitCode != 0 {
+ g.Log().Error(ctx, "Failed to reload postfix:", err, result)
+ res.SetError(gerror.New(public.LangCtx(ctx, "set config success but reload failed, please check service status")))
+ return res, nil
+ }
+
+ configLog := make([]string, 0)
+ for key, value := range validConfigs {
+ configLog = append(configLog, key+"="+value)
+ }
+ _ = public.WriteLog(ctx, public.LogParams{
+ Type: consts.LOGTYPE.Settings,
+ Log: "Set postfix configs [" + strings.Join(configLog, ", ") + "] successfully",
+ })
+
+ res.SetSuccess(public.LangCtx(ctx, "set postfix configs success"))
+ return res, nil
+}