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 operation of email queue, overview page adds email delay count

lotk6df234849567
diff --git a/core/api/mail_services/v1/postfix_queue.go b/core/api/mail_services/v1/postfix_queue.go
index 2fb87272..b46d602e 100644
--- a/core/api/mail_services/v1/postfix_queue.go
+++ b/core/api/mail_services/v1/postfix_queue.go
@@ -47,9 +47,9 @@ type GetPostfixQueueInfoRes struct {
}
type DeletePostfixQueueByIdReq struct {
- g.Meta `path:"/postfix_queue/delete_by_id" method:"post" summary:"Delete specified queue mail"`
- Authorization string `json:"authorization" dc:"Authorization" in:"header"`
- QueueID string `json:"queue_id" v:"required" dc:"Queue ID"`
+ g.Meta `path:"/postfix_queue/delete_by_id" method:"post" summary:"Delete specified queue mails (batch supported)"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ QueueIDs []string `json:"queue_ids" v:"required" dc:"Queue ID list"`
}
type DeletePostfixQueueByIdRes struct {
@@ -76,9 +76,9 @@ type FlushPostfixQueueRes struct {
}
type FlushPostfixQueueByIdReq struct {
- g.Meta `path:"/postfix_queue/flush_by_id" method:"post" summary:"Resend specified queue mail"`
- Authorization string `json:"authorization" dc:"Authorization" in:"header"`
- QueueID string `json:"queue_id" v:"required" dc:"Queue ID"`
+ g.Meta `path:"/postfix_queue/flush_by_id" method:"post" summary:"Flush specified queue mails (batch supported)"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ QueueIDs []string `json:"queue_ids" v:"required" dc:"Queue ID list"`
}
type FlushPostfixQueueByIdRes struct {
diff --git a/core/internal/controller/mail_services/mail_services_v1_delete_postfix_queue_by_id.go b/core/internal/controller/mail_services/mail_services_v1_delete_postfix_queue_by_id.go
index ef072f16..78a1a819 100644
--- a/core/internal/controller/mail_services/mail_services_v1_delete_postfix_queue_by_id.go
+++ b/core/internal/controller/mail_services/mail_services_v1_delete_postfix_queue_by_id.go
@@ -6,6 +6,7 @@ import (
"billionmail-core/internal/service/public"
"context"
"github.com/gogf/gf/v2/errors/gerror"
+ "strings"
"billionmail-core/api/mail_services/v1"
)
@@ -14,22 +15,43 @@ func (c *ControllerV1) DeletePostfixQueueById(ctx context.Context, req *v1.Delet
res = &v1.DeletePostfixQueueByIdRes{}
dockerApi, err := docker.NewDockerAPI()
-
if err != nil {
res.SetError(gerror.New(public.LangCtx(ctx, "docker api unavailable: {}", err.Error())))
return res, nil
}
- _, err = dockerApi.ExecCommandByName(ctx, "postfix", []string{"postsuper", "-d", req.QueueID}, "root")
- if err != nil {
- res.SetError(gerror.New(public.LangCtx(ctx, "postsuper -d failed: {}", err.Error())))
+
+ if len(req.QueueIDs) == 0 {
+ res.SetError(gerror.New(public.LangCtx(ctx, "No queue IDs provided")))
return res, nil
}
- // Operation Log
+
+ failed := make([]string, 0)
+ success := make([]string, 0)
+ for _, queueID := range req.QueueIDs {
+ _, err = dockerApi.ExecCommandByName(ctx, "postfix", []string{"postsuper", "-d", queueID}, "root")
+ if err != nil {
+ failed = append(failed, queueID)
+ continue
+ }
+ success = append(success, queueID)
+ }
+
+ if len(failed) > 0 {
+ if len(success) > 0 {
+ _ = public.WriteLog(ctx, public.LogParams{
+ Type: consts.LOGTYPE.PostfixQueue,
+ Log: "Delete postfix queue mail " + strings.Join(success, ",") + " successfully",
+ })
+ }
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to delete some queue mails: {}", failed)))
+ return res, nil
+ }
+
_ = public.WriteLog(ctx, public.LogParams{
Type: consts.LOGTYPE.PostfixQueue,
- Log: "Delete postfix queue mail " + req.QueueID + " successfully",
+ Log: "Delete postfix queue mail " + strings.Join(req.QueueIDs, ",") + " successfully",
})
- res.SetSuccess(public.LangCtx(ctx, "delete postfix queue mail success"))
+ res.SetSuccess(public.LangCtx(ctx, "delete postfix queue mails success"))
return res, nil
}
diff --git a/core/internal/controller/mail_services/mail_services_v1_flush_postfix_queue_by_id.go b/core/internal/controller/mail_services/mail_services_v1_flush_postfix_queue_by_id.go
index 7abc584d..b9ebcefe 100644
--- a/core/internal/controller/mail_services/mail_services_v1_flush_postfix_queue_by_id.go
+++ b/core/internal/controller/mail_services/mail_services_v1_flush_postfix_queue_by_id.go
@@ -15,8 +15,8 @@ import (
func (c *ControllerV1) FlushPostfixQueueById(ctx context.Context, req *v1.FlushPostfixQueueByIdReq) (res *v1.FlushPostfixQueueByIdRes, err error) {
res = &v1.FlushPostfixQueueByIdRes{}
- if req.QueueID == "" {
- res.SetError(gerror.New(public.LangCtx(ctx, "queue_id required")))
+ if len(req.QueueIDs) == 0 {
+ res.SetError(gerror.New(public.LangCtx(ctx, "queue_ids required")))
return res, nil
}
dk, dockerErr := docker.NewDockerAPI()
@@ -26,28 +26,34 @@ func (c *ControllerV1) FlushPostfixQueueById(ctx context.Context, req *v1.FlushP
return res, nil
}
defer dk.Close()
- cmd := []string{"postqueue", "-i", req.QueueID}
- result, err := dk.ExecCommandByName(ctx, consts.SERVICES.Postfix, cmd, "root")
- if err != nil {
- g.Log().Error(ctx, "Failed to execute command in container :", err, "command:", strings.Join(cmd, " "))
- res.SetError(gerror.New(public.LangCtx(ctx, "Failed to execute command in container: {}", err.Error())))
- return res, nil
- }
- if result == nil {
- g.Log().Error(ctx, "The result of the command is empty:", "command:", strings.Join(cmd, " "))
- res.SetError(gerror.New(public.LangCtx(ctx, "The result of the command is empty: {}", strings.Join(cmd, " "))))
- return res, nil
+
+ failed := make([]string, 0)
+ success := make([]string, 0)
+ for _, queueID := range req.QueueIDs {
+ cmd := []string{"postqueue", "-i", queueID}
+ result, err := dk.ExecCommandByName(ctx, consts.SERVICES.Postfix, cmd, "root")
+ if err != nil || result == nil || result.ExitCode != 0 {
+ failed = append(failed, queueID)
+ continue
+ }
+ success = append(success, queueID)
}
- if result.ExitCode != 0 {
- g.Log().Error(ctx, "The command execution returns a nonzero status:", result.ExitCode, "output:", result.Output, "command:", strings.Join(cmd, " "))
- res.SetError(gerror.New(public.LangCtx(ctx, "Command execution failed, exit code: {}, output: {}", result.ExitCode, result.Output)))
+
+ if len(failed) > 0 {
+ if len(success) > 0 {
+ _ = public.WriteLog(ctx, public.LogParams{
+ Type: consts.LOGTYPE.PostfixQueue,
+ Log: "Flush postfix queue mail [" + strings.Join(success, ",") + "] successfully",
+ })
+ }
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to flush some queue mails: {}", failed)))
return res, nil
}
- // Operation Log
+
_ = public.WriteLog(ctx, public.LogParams{
Type: consts.LOGTYPE.PostfixQueue,
- Log: "Flush postfix queue mail [" + req.QueueID + "] successfully",
+ Log: "Flush postfix queue mail [" + strings.Join(req.QueueIDs, ",") + "] successfully",
})
- res.SetSuccess(public.LangCtx(ctx, "flush postfix queue mail by id success"))
+ res.SetSuccess(public.LangCtx(ctx, "flush postfix queue mails by id success"))
return res, nil
}
diff --git a/core/internal/service/maillog_stat/overview.go b/core/internal/service/maillog_stat/overview.go
index e2493534..80ea27d0 100644
--- a/core/internal/service/maillog_stat/overview.go
+++ b/core/internal/service/maillog_stat/overview.go
@@ -1,12 +1,17 @@
package maillog_stat
import (
+ "billionmail-core/internal/consts"
+ docker "billionmail-core/internal/service/dockerapi"
"billionmail-core/internal/service/public"
"context"
"github.com/gogf/gf/v2/database/gdb"
+ "github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
+ "github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/util/gconv"
"sort"
+ "strings"
"time"
)
@@ -80,11 +85,12 @@ func (o *Overview) overviewDashboard(campaignID int64, domain string, startTime,
query.Fields("coalesce(sum(case when status='bounced' then 1 else 0 end), 0) as bounced")
aggregate := map[string]interface{}{
- "sends": 0,
- "delivered": 0,
- "opened": 0,
- "clicked": 0,
- "bounced": 0,
+ "sends": 0,
+ "delivered": 0,
+ "opened": 0,
+ "clicked": 0,
+ "bounced": 0,
+ "delayed_queue": 0,
}
result, err := query.One()
@@ -116,9 +122,57 @@ func (o *Overview) overviewDashboard(campaignID int64, domain string, startTime,
aggregate["click_rate"] = 0
}
+ // delayed_queue
+ aggregate["delayed_queue"] = 0
+ delayedCount, err := o.getPostfixDeferredQueueCount(context.Background())
+ if err == nil {
+ aggregate["delayed_queue"] = delayedCount
+ }
+
return aggregate
}
+// Obtain the number of deferred queues with a 1-minute cache
+func (o *Overview) getPostfixDeferredQueueCount(ctx context.Context) (int, error) {
+ cache := gcache.New()
+ cacheKey := "postfix_deferred_queue_count"
+ if v, err := cache.Get(ctx, cacheKey); err == nil && v != nil {
+ return v.Int(), nil
+ }
+
+ dk, err := docker.NewDockerAPI()
+ if err != nil {
+ return 0, err
+ }
+ defer dk.Close()
+
+ cmd := []string{"postqueue", "-j"}
+ result, err := dk.ExecCommandByName(ctx, consts.SERVICES.Postfix, cmd, "root")
+ if err != nil || result == nil || result.ExitCode != 0 {
+ return 0, err
+ }
+
+ lines := strings.Split(result.Output, "\n")
+ count := 0
+ for _, line := range lines {
+ if strings.TrimSpace(line) == "" {
+ continue
+ }
+ var item struct {
+ QueueName string `json:"queue_name"`
+ }
+ if err := gjson.Unmarshal([]byte(line), &item); err != nil {
+ continue
+ }
+ if item.QueueName == "deferred" {
+ count++
+ }
+ }
+
+ _ = cache.Set(ctx, cacheKey, count, time.Minute)
+ return count, nil
+}
+
// overviewProviders statistic mail provider
func (o *Overview) overviewProviders(campaignID int64, domain string, startTime, endTime int64) []map[string]interface{} {
startTime, endTime = o.filterAndPrepareTimeSection(startTime, endTime)