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

Adjust the unsubscribe button to add logic

lotk3d23f623564a
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_send_test_email.go b/core/internal/controller/batch_mail/batch_mail_v1_send_test_email.go
index 6890d116..1c660c79 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_send_test_email.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_send_test_email.go
@@ -39,6 +39,11 @@ func (c *ControllerV1) SendTestEmail(ctx context.Context, req *v1.SendTestEmailR
return
}
+ // --------------------------------------
+ if !strings.Contains(template.Content, "__UNSUBSCRIBE_URL__") {
+ template.Content = public.AddUnsubscribeButton(template.Content)
+ }
+
domain := domains.GetBaseURL()
// generate unsubscribe URL placeholder
@@ -64,6 +69,7 @@ func (c *ControllerV1) SendTestEmail(ctx context.Context, req *v1.SendTestEmailR
content = strings.ReplaceAll(content, "__JWT__", jwtToken)
content = strings.ReplaceAll(content, "__EMAIL__", req.Recipient)
}
+ // --------------------------------------
personalizedContent := content
diff --git a/core/internal/controller/email_template/email_template_v1_create_template.go b/core/internal/controller/email_template/email_template_v1_create_template.go
index 1d4afe3d..8f9049f5 100644
--- a/core/internal/controller/email_template/email_template_v1_create_template.go
+++ b/core/internal/controller/email_template/email_template_v1_create_template.go
@@ -3,9 +3,6 @@ package email_template
import (
"billionmail-core/internal/service/public"
"context"
- "fmt"
- "strings"
-
"github.com/gogf/gf/v2/errors/gerror"
"billionmail-core/api/email_template/v1"
@@ -45,8 +42,7 @@ func (c *ControllerV1) CreateTemplate(ctx context.Context, req *v1.CreateTemplat
return
}
- // Add unsubscribe button
- content = addUnsubscribeButton(content)
+ //content = public.AddUnsubscribeButton(content)
} else { // Drag to generate
if req.Content == "" || req.Render == "" {
res.Code = 400
@@ -69,29 +65,3 @@ func (c *ControllerV1) CreateTemplate(ctx context.Context, req *v1.CreateTemplat
res.SetSuccess(public.LangCtx(ctx, "Template created successfully"))
return
}
-
-// addUnsubscribeButton
-func addUnsubscribeButton(content string) string {
- // Unsubscribe button HTML
- unsubscribeButton := `<div style="padding: 16px 0; text-align: center"><a href="__UNSUBSCRIBE_URL__" style="color: #ccc; font-size: 12px">Unsubscribe</a></div>`
-
- // If content already contains unsubscribe link, return directly
- if strings.Contains(content, "__UNSUBSCRIBE_URL__") {
- return content
- }
-
- // check if content has body tag
- hasBodyTag := strings.Contains(content, "</body>")
- hasHtmlTag := strings.Contains(content, "</html>")
-
- if hasBodyTag {
- return strings.Replace(content, "</body>", unsubscribeButton+"</body>", 1)
- }
-
- if hasHtmlTag {
- return strings.Replace(content, "</html>", unsubscribeButton+"</html>", 1)
- }
-
- // if content has no body tag or html tag, add a complete html framework
- return fmt.Sprintf(`<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div style="width: 100%%; max-width: 600px; margin: 0 auto;">%s%s</div></body></html>`, content, unsubscribeButton)
-}
diff --git a/core/internal/service/batch_mail/task_executor.go b/core/internal/service/batch_mail/task_executor.go
index 049ef27d..f172c5a0 100644
--- a/core/internal/service/batch_mail/task_executor.go
+++ b/core/internal/service/batch_mail/task_executor.go
@@ -4,6 +4,7 @@ import (
"billionmail-core/internal/model/entity"
"billionmail-core/internal/service/domains"
"billionmail-core/internal/service/mail_service"
+ "billionmail-core/internal/service/public"
"context"
"errors"
"fmt"
@@ -858,6 +859,10 @@ func (e *TaskExecutor) getTemplateInfo(ctx context.Context, templateId int) (*en
func (e *TaskExecutor) processEmailContent(ctx context.Context, content string, task *entity.EmailTask) string {
// process unsubscribe link
if task.Unsubscribe == 1 {
+ // __UNSUBSCRIBE_URL__
+ if !strings.Contains(content, "__UNSUBSCRIBE_URL__") {
+ content = public.AddUnsubscribeButton(content)
+ }
// get current domain
domain := domains.GetBaseURL()
diff --git a/core/internal/service/public/common.go b/core/internal/service/public/common.go
index ac15a243..49c97366 100644
--- a/core/internal/service/public/common.go
+++ b/core/internal/service/public/common.go
@@ -2449,3 +2449,28 @@ func SanitizeUTF8(s string) string {
return string(result)
}
+
+func AddUnsubscribeButton(content string) string {
+ // Unsubscribe button HTML
+ unsubscribeButton := `<div style="padding: 16px 0; text-align: center"><a href="__UNSUBSCRIBE_URL__" style="color: #ccc; font-size: 12px">Unsubscribe</a></div>`
+
+ // If content already contains unsubscribe link, return directly
+ if strings.Contains(content, "__UNSUBSCRIBE_URL__") {
+ return content
+ }
+
+ // check if content has body tag
+ hasBodyTag := strings.Contains(content, "</body>")
+ hasHtmlTag := strings.Contains(content, "</html>")
+
+ if hasBodyTag {
+ return strings.Replace(content, "</body>", unsubscribeButton+"</body>", 1)
+ }
+
+ if hasHtmlTag {
+ return strings.Replace(content, "</html>", unsubscribeButton+"</html>", 1)
+ }
+
+ // if content has no body tag or html tag, add a complete html framework
+ return fmt.Sprintf(`<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div style="width: 100%%; max-width: 600px; margin: 0 auto;">%s%s</div></body></html>`, content, unsubscribeButton)
+}