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

update blacklist alert settings structure and validation

lotka07257aded73
diff --git a/core/api/settings/v1/settings.go b/core/api/settings/v1/settings.go
index 18a5398c..a3ee91af 100644
--- a/core/api/settings/v1/settings.go
+++ b/core/api/settings/v1/settings.go
@@ -280,8 +280,13 @@ type SetBlacklistAlertRes struct {
type SetBlacklistAlertSettingsReq struct {
g.Meta `path:"/settings/set_blacklist_alert_settings" tags:"Settings" method:"post" summary:"Set blacklist alert settings"`
- Authorization string `json:"authorization" dc:"Authorization" in:"header"`
- Settings BlacklistAlertSettings `json:"settings" dc:"Alarm settings" v:"required"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ Name string `json:"name" dc:"Alarm sender's name" v:"required"`
+ SenderEmail string `json:"sender_email" dc:"sender email" v:"required|email" v:"required"`
+ SMTPPassword string `json:"smtp_password" dc:"SMTP password" v:"required" v:"required"`
+ SMTPServer string `json:"smtp_server" dc:"SMTP server" v:"required" v:"required"`
+ SMTPPort int `json:"smtp_port" dc:"SMTP port" v:"required|between:1,65535" d:"587" v:"required"`
+ RecipientList []string `json:"recipient_list" dc:"recipient list" v:"required"`
}
type SetBlacklistAlertSettingsRes struct {
diff --git a/core/internal/controller/settings/settings_v1_set_blacklist_alert_settings.go b/core/internal/controller/settings/settings_v1_set_blacklist_alert_settings.go
index b644fb62..89f7c7e3 100644
--- a/core/internal/controller/settings/settings_v1_set_blacklist_alert_settings.go
+++ b/core/internal/controller/settings/settings_v1_set_blacklist_alert_settings.go
@@ -19,26 +19,35 @@ import (
func (c *ControllerV1) SetBlacklistAlertSettings(ctx context.Context, req *v1.SetBlacklistAlertSettingsReq) (res *v1.SetBlacklistAlertSettingsRes, err error) {
res = &v1.SetBlacklistAlertSettingsRes{}
- if err := validateAlertSettings(&req.Settings); err != nil {
+ if err := validateAlertSettings(req); err != nil {
res.SetError(gerror.Newf(public.LangCtx(ctx, "Parameter validation failed: {}", err.Error())))
return res, nil
}
//g.Log().Info(ctx, "Testing SMTP connection...")
- if err := testSMTPConnectionWithRelay(ctx, &req.Settings); err != nil {
+ if err := testSMTPConnectionWithRelay(ctx, req); err != nil {
res.SetError(gerror.Newf(public.LangCtx(ctx, "SMTP connection test failed: {}", err.Error())))
return res, nil
}
//g.Log().Info(ctx, "Sending test email...")
- if err := sendTestEmailWithMailService(ctx, &req.Settings); err != nil {
+ if err := sendTestEmailWithMailService(ctx, req); err != nil {
res.SetError(gerror.Newf(public.LangCtx(ctx, "Failed to send test email: {}", err.Error())))
return res, nil
}
alertSettingsFile := public.AbsPath("../core/data/blacklist_alert_settings.json")
- jsonData, err := json.MarshalIndent(req.Settings, "", " ")
+ data := g.Map{
+ "name": req.Name,
+ "sender_email": req.SenderEmail,
+ "smtp_password": req.SMTPPassword,
+ "smtp_server": req.SMTPServer,
+ "smtp_port": req.SMTPPort,
+ "recipient_list": req.RecipientList,
+ }
+
+ jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
res.SetError(gerror.Newf(public.LangCtx(ctx, "Failed to encode alert settings: {}", err.Error())))
return res, nil
@@ -54,7 +63,7 @@ func (c *ControllerV1) SetBlacklistAlertSettings(ctx context.Context, req *v1.Se
return res, nil
}
-func validateAlertSettings(settings *v1.BlacklistAlertSettings) error {
+func validateAlertSettings(settings *v1.SetBlacklistAlertSettingsReq) error {
if settings.SenderEmail == "" {
return fmt.Errorf("sender email is required")
}
@@ -88,7 +97,7 @@ func validateAlertSettings(settings *v1.BlacklistAlertSettings) error {
return nil
}
-func testSMTPConnectionWithRelay(ctx context.Context, settings *v1.BlacklistAlertSettings) error {
+func testSMTPConnectionWithRelay(ctx context.Context, settings *v1.SetBlacklistAlertSettingsReq) error {
//g.Log().Infof(ctx, "Testing SMTP connection to %s:%d", settings.SMTPServer, settings.SMTPPort)
result := relay.TestSmtpConnection(
@@ -106,7 +115,7 @@ func testSMTPConnectionWithRelay(ctx context.Context, settings *v1.BlacklistAler
return nil
}
-func sendTestEmailWithMailService(ctx context.Context, settings *v1.BlacklistAlertSettings) error {
+func sendTestEmailWithMailService(ctx context.Context, settings *v1.SetBlacklistAlertSettingsReq) error {
//g.Log().Infof(ctx, "Sending test email to %v", settings.RecipientList)
subject := "Blacklist Alert Test - BillionMail"
@@ -150,7 +159,7 @@ func sendTestEmailWithMailService(ctx context.Context, settings *v1.BlacklistAle
return nil
}
-func buildTestEmailHTML(settings *v1.BlacklistAlertSettings) string {
+func buildTestEmailHTML(settings *v1.SetBlacklistAlertSettingsReq) string {
return fmt.Sprintf(`
<html>
<head>