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

Check the content of the email and adjust the time zone

lotk8b92a332bb81
diff --git a/core/api/email_template/email_template.go b/core/api/email_template/email_template.go
index 51280ced..0e7c7913 100644
--- a/core/api/email_template/email_template.go
+++ b/core/api/email_template/email_template.go
@@ -18,4 +18,5 @@ type IEmailTemplateV1 interface {
CopyTemplate(ctx context.Context, req *v1.CopyTemplateReq) (res *v1.CopyTemplateRes, err error)
GetTemplate(ctx context.Context, req *v1.GetTemplateReq) (res *v1.GetTemplateRes, err error)
GetAllTemplates(ctx context.Context, req *v1.GetAllTemplatesReq) (res *v1.GetAllTemplatesRes, err error)
+ CheckEmailContent(ctx context.Context, req *v1.CheckEmailContentReq) (res *v1.CheckEmailContentRes, err error)
}
diff --git a/core/api/email_template/v1/email_template.go b/core/api/email_template/v1/email_template.go
index 19a058f6..92e480af 100644
--- a/core/api/email_template/v1/email_template.go
+++ b/core/api/email_template/v1/email_template.go
@@ -105,3 +105,23 @@ type GetAllTemplatesReq struct {
type GetAllTemplatesRes struct {
api_v1.StandardRes
}
+
+type ScoreItem struct {
+ Name string `json:"name"`
+ Detail string `json:"detail"`
+ Score float64 `json:"score"`
+}
+
+type CheckEmailContentReq struct {
+ g.Meta `path:"/email_template/check" method:"post" tags:"EmailTemplate" summary:"Check email content"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ Content string `json:"content" dc:"Email Content"`
+}
+
+type CheckEmailContentRes struct {
+ api_v1.StandardRes
+ Data struct {
+ Score float64 `json:"score"`
+ Items []ScoreItem `json:"items"`
+ } `json:"data"`
+}
diff --git a/core/api/settings/settings.go b/core/api/settings/settings.go
index 61abda6d..d6e1702a 100644
--- a/core/api/settings/settings.go
+++ b/core/api/settings/settings.go
@@ -16,4 +16,5 @@ type ISettingsV1 interface {
SetSystemConfig(ctx context.Context, req *v1.SetSystemConfigReq) (res *v1.SetSystemConfigRes, err error)
SetSystemConfigKey(ctx context.Context, req *v1.SetSystemConfigKeyReq) (res *v1.SetSystemConfigKeyRes, err error)
SetSSLConfig(ctx context.Context, req *v1.SetSSLConfigReq) (res *v1.SetSSLConfigRes, err error)
+ GetTimeZoneList(ctx context.Context, req *v1.GetTimeZoneListReq) (res *v1.GetTimeZoneListRes, err error)
}
diff --git a/core/api/settings/v1/settings.go b/core/api/settings/v1/settings.go
index f766c032..f17f2f98 100644
--- a/core/api/settings/v1/settings.go
+++ b/core/api/settings/v1/settings.go
@@ -64,9 +64,8 @@ type SystemConfig struct {
// Time zone configuration
ManageTimeZone struct {
- TimeZone string `json:"timezone" dc:"time zone"`
- Command string `json:"command" dc:"command"`
- AllTimeZones map[string][]string `json:"zones" dc:"available time zones"`
+ TimeZone string `json:"timezone" dc:"time zone"`
+ Command string `json:"command" dc:"command"`
} `json:"manage_timezone" dc:"time zone configuration"`
// IPv4 network configuration
@@ -135,3 +134,11 @@ type SetSSLConfigReq struct {
type SetSSLConfigRes struct {
api_v1.StandardRes
}
+
+type GetTimeZoneListReq struct {
+ g.Meta `path:"/settings/get_timezone_list" tags:"Settings" method:"get" summary:"Get available time zones"`
+}
+type GetTimeZoneListRes struct {
+ api_v1.StandardRes
+ Data map[string][]string `json:"data" dc:"available time zones"`
+}
diff --git a/core/internal/controller/email_template/email_template_v1_check_email_content.go b/core/internal/controller/email_template/email_template_v1_check_email_content.go
new file mode 100644
index 00000000..9a6ec826
--- /dev/null
+++ b/core/internal/controller/email_template/email_template_v1_check_email_content.go
@@ -0,0 +1,138 @@
+package email_template
+
+import (
+ "billionmail-core/internal/service/public"
+ "context"
+ "fmt"
+ "regexp"
+ "strings"
+
+ "billionmail-core/api/email_template/v1"
+)
+
+type ScoreItem struct {
+ Name string `json:"name"`
+ Detail string `json:"detail"`
+ Score float64 `json:"score"`
+}
+
+var spamWords = []string{
+ // Forbidden words in Chinese
+ "免费", "中奖", "现金", "优惠", "点击这里", "立即领取", "限时特惠", "最后机会", "免费试用",
+ "恭喜您", "大奖", "赢取", "抽奖", "红包", "免单", "清仓", "秒杀", "特价", "折扣",
+ "投资回报", "高收益", "稳赚不赔", "快速赚钱", "轻松赚钱", "暴利", "一本万利",
+
+ // Forbidden words in English
+ "buy now", "free", "winner", "urgent", "limited time", "click here",
+ "act now", "apply now", "call now", "don't delete", "exclusive deal",
+ "for free", "get it now", "limited offer", "new customers only",
+ "order now", "special promotion", "time limited", "while supplies last",
+ "earn money", "make money", "extra income", "work from home",
+}
+
+var shortLinkDomains = []string{
+ // International short chain
+ "bit.ly", "tinyurl.com", "goo.gl", "ow.ly", "t.co", "is.gd",
+ "buff.ly", "adf.ly", "shorte.st", "bc.vc", "cutt.ly", "shorturl.at",
+
+ "t.cn", "sinaurl.cn", "dwz.cn", "url.cn", "suo.im", "mrw.so",
+ "x.co", "zzb.bz", "985.so", "6du.in", "ft12.com", "url7.me",
+
+ // other short chain
+ "rebrand.ly", "bit.do", "short.io", "shrink.me", "short.cm",
+}
+
+func isHTMLOK(content string) bool {
+ // Checks if the HTML tag is closed
+ openTags := regexp.MustCompile(`<([a-zA-Z]+)[^>]*?>`).FindAllStringSubmatch(content, -1)
+ closeTags := regexp.MustCompile(`</([a-zA-Z]+)>`).FindAllStringSubmatch(content, -1)
+ openCount := make(map[string]int)
+ closeCount := make(map[string]int)
+ for _, tag := range openTags {
+ openCount[strings.ToLower(tag[1])]++
+ }
+ for _, tag := range closeTags {
+ closeCount[strings.ToLower(tag[1])]++
+ }
+ for tag, count := range openCount {
+ if closeCount[tag] != count {
+ return false
+ }
+ }
+ return true
+}
+
+func isImageOnly(content string) bool {
+ imgTag := regexp.MustCompile(`<img[\s\S]*?>`)
+ text := regexp.MustCompile(`<.*?>`).ReplaceAllString(content, "")
+ return imgTag.MatchString(content) && len(strings.TrimSpace(text)) < 10
+}
+
+func hasShortLink(content string) bool {
+ for _, domain := range shortLinkDomains {
+ if strings.Contains(content, domain) {
+ return true
+ }
+ }
+ return false
+}
+func imgWithoutAltCount(content string) (count int) {
+ imgTag := regexp.MustCompile(`<img[^>]*?>`)
+ altAttr := regexp.MustCompile(`alt\s*=\s*['\"]`)
+ imgs := imgTag.FindAllString(content, -1)
+ for _, img := range imgs {
+ if !altAttr.MatchString(img) {
+ count++
+ }
+ }
+ return
+}
+func (c *ControllerV1) CheckEmailContent(ctx context.Context, req *v1.CheckEmailContentReq) (res *v1.CheckEmailContentRes, err error) {
+ res = &v1.CheckEmailContentRes{}
+ var totalScore float64 = 10.0
+ var items []ScoreItem
+ content := req.Content
+
+ // 1. Spam detection
+ for _, word := range spamWords {
+ if strings.Contains(strings.ToLower(content), word) {
+ items = append(items, ScoreItem{"Spam Word", fmt.Sprintf("Contains spam word: %s", word), -1.0})
+ totalScore -= 1.0
+ }
+ }
+ // 2. HTML standard check
+ if !isHTMLOK(content) {
+ items = append(items, ScoreItem{"HTML Format", "HTML tags are not standard", -0.5})
+ totalScore -= 0.5
+ }
+ if isImageOnly(content) {
+ items = append(items, ScoreItem{"Image Ratio", "Content is purely images or image-heavy", -1.0})
+ totalScore -= 1.0
+ }
+ // 2.1 Check for missing alt attributes in img tags
+ imgNoAlt := imgWithoutAltCount(content)
+ if imgNoAlt > 0 {
+ items = append(items, ScoreItem{"Missing Alt Attribute", fmt.Sprintf("%d <img> tags are missing the alt attribute", imgNoAlt), -0.2 * float64(imgNoAlt)})
+ totalScore -= 0.2 * float64(imgNoAlt)
+ }
+ // 3. Link check
+ if hasShortLink(content) {
+ items = append(items, ScoreItem{"Short Link", "Contains a short link domain", -0.5})
+ totalScore -= 0.5
+ }
+
+ if totalScore < 0 {
+ totalScore = 0
+ }
+
+ res.Data.Score = totalScore
+ for _, item := range items {
+ res.Data.Items = append(res.Data.Items, v1.ScoreItem{
+ Name: item.Name,
+ Detail: item.Detail,
+ Score: item.Score,
+ })
+ }
+ res.SetSuccess(public.LangCtx(ctx, "Successful"))
+ return res, nil
+}
diff --git a/core/internal/controller/settings/settings.go b/core/internal/controller/settings/settings.go
index 9cfa496d..fb178d85 100644
--- a/core/internal/controller/settings/settings.go
+++ b/core/internal/controller/settings/settings.go
@@ -9,23 +9,18 @@ import (
"billionmail-core/internal/consts"
"billionmail-core/internal/service/public"
"crypto/x509"
- "encoding/json"
"encoding/pem"
"fmt"
- "github.com/gogf/gf/v2/os/gfile"
"os"
"path/filepath"
- "sort"
"strconv"
"strings"
"time"
)
var (
- //envPath = "../.env"
envPath = public.AbsPath("../.env")
projectPath = public.AbsPath("../")
- zoneFile = public.AbsPath("../core/data/zoneinfo_list.json")
)
// loadEnvFile .env
@@ -187,13 +182,6 @@ func convertEnvToConfig(envMap map[string]string) *v1.SystemConfig {
config.ManageTimeZone.TimeZone = envMap["TZ"]
config.ManageTimeZone.Command = fmt.Sprintf("cd %s && echo \"TIME ZONE\" | bash bm.sh change-tz", projectPath)
- group, err := loadTimeZoneGroupFromFile(zoneFile)
- if err != nil || len(group.ZoneList) == 0 {
- group, _ = getTimeZoneGroup()
- err = saveTimeZoneGroupToFile(group, zoneFile)
- }
- config.ManageTimeZone.AllTimeZones = group.Zones
-
// Network configuration
config.IPv4Network = envMap["IPV4_NETWORK"]
config.Fail2ban = envMap["FAIL2BAN_INIT"] == "y"
@@ -201,55 +189,6 @@ func convertEnvToConfig(envMap map[string]string) *v1.SystemConfig {
return config
}
-type TimeZoneGroup struct {
- ZoneList []string `json:"zoneList"`
- Zones map[string][]string `json:"zones"`
-}
-
-func getTimeZoneGroup() (TimeZoneGroup, error) {
- zoneList := []string{"Asia", "Africa", "America", "Antarctica", "Arctic", "Atlantic", "Australia", "Europe", "Indian", "Pacific"}
- zones := make(map[string][]string)
- root := "/usr/share/zoneinfo"
-
- for _, mainZone := range zoneList {
- dir := filepath.Join(root, mainZone)
- files, err := os.ReadDir(dir)
- if err != nil {
- continue
- }
- for _, f := range files {
- if !f.IsDir() {
- zones[mainZone] = append(zones[mainZone], f.Name())
- }
- }
- sort.Strings(zones[mainZone])
- }
- return TimeZoneGroup{
- ZoneList: zoneList,
- Zones: zones,
- }, nil
-}
-
-func saveTimeZoneGroupToFile(group TimeZoneGroup, filePath string) error {
- data, err := json.Marshal(group)
- if err != nil {
- return err
- }
- return gfile.PutContents(filePath, string(data))
-}
-
-func loadTimeZoneGroupFromFile(filePath string) (TimeZoneGroup, error) {
- var group TimeZoneGroup
- if !gfile.Exists(filePath) {
- return group, os.ErrNotExist
- }
- data := gfile.GetContents(filePath)
- if err := json.Unmarshal([]byte(data), &group); err != nil {
- return group, err
- }
- return group, nil
-}
-
// parseInt Safely convert string to integer
func parseInt(s string, defaultValue int) int {
if v, err := strconv.Atoi(s); err == nil {
diff --git a/core/internal/controller/settings/settings_v1_get_time_zone_list.go b/core/internal/controller/settings/settings_v1_get_time_zone_list.go
new file mode 100644
index 00000000..6cab7e1e
--- /dev/null
+++ b/core/internal/controller/settings/settings_v1_get_time_zone_list.go
@@ -0,0 +1,76 @@
+package settings
+
+import (
+ "billionmail-core/api/settings/v1"
+ "billionmail-core/internal/service/public"
+ "context"
+ "encoding/json"
+ "github.com/gogf/gf/v2/os/gfile"
+ "os"
+ "path/filepath"
+ "sort"
+)
+
+func (c *ControllerV1) GetTimeZoneList(ctx context.Context, req *v1.GetTimeZoneListReq) (res *v1.GetTimeZoneListRes, err error) {
+ res = &v1.GetTimeZoneListRes{}
+
+ zoneFile := public.AbsPath("../core/data/zoneinfo_list.json")
+ group, err := loadTimeZoneGroupFromFile(zoneFile)
+ if err != nil || len(group.ZoneList) == 0 {
+ group, _ = getTimeZoneGroup()
+ err = saveTimeZoneGroupToFile(group, zoneFile)
+ }
+
+ res.Data = group.Zones
+ res.SetSuccess(public.LangCtx(ctx, "Successful"))
+ return res, nil
+}
+
+type TimeZoneGroup struct {
+ ZoneList []string `json:"zoneList"`
+ Zones map[string][]string `json:"zones"`
+}
+
+func getTimeZoneGroup() (TimeZoneGroup, error) {
+ zoneList := []string{"Asia", "Africa", "America", "Antarctica", "Arctic", "Atlantic", "Australia", "Europe", "Indian", "Pacific"}
+ zones := make(map[string][]string)
+ root := "/usr/share/zoneinfo"
+
+ for _, mainZone := range zoneList {
+ dir := filepath.Join(root, mainZone)
+ files, err := os.ReadDir(dir)
+ if err != nil {
+ continue
+ }
+ for _, f := range files {
+ if !f.IsDir() {
+ zones[mainZone] = append(zones[mainZone], f.Name())
+ }
+ }
+ sort.Strings(zones[mainZone])
+ }
+ return TimeZoneGroup{
+ ZoneList: zoneList,
+ Zones: zones,
+ }, nil
+}
+
+func saveTimeZoneGroupToFile(group TimeZoneGroup, filePath string) error {
+ data, err := json.Marshal(group)
+ if err != nil {
+ return err
+ }
+ return gfile.PutContents(filePath, string(data))
+}
+
+func loadTimeZoneGroupFromFile(filePath string) (TimeZoneGroup, error) {
+ var group TimeZoneGroup
+ if !gfile.Exists(filePath) {
+ return group, os.ErrNotExist
+ }
+ data := gfile.GetContents(filePath)
+ if err := json.Unmarshal([]byte(data), &group); err != nil {
+ return group, err
+ }
+ return group, nil
+}