diff --git a/internal/email/email.go b/internal/email/email.go index 7798ba4..5305330 100644 --- a/internal/email/email.go +++ b/internal/email/email.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/url" + "strings" "time" "github.com/rideaware/admin-panel/internal/config" @@ -77,10 +78,8 @@ func send(subject, body, recipient string) bool { unsubLink := fmt.Sprintf("https://%s/unsubscribe?email=%s", cfg.BaseURL, url.QueryEscape(recipient)) - htmlBody := fmt.Sprintf( - "%s

If you ever wish to unsubscribe, "+ - "please click here", - body, unsubLink) + // Build HTML body with unsubscribe link + htmlBody := buildHTMLBody(body, unsubLink) m.SetBodyString(mail.TypeTextHTML, htmlBody) if err := client.Send(m); err != nil { @@ -90,4 +89,26 @@ func send(subject, body, recipient string) bool { log.Printf("Update email sent to: %s", recipient) return true +} + +// buildHTMLBody constructs the final HTML email body by appending an unsubscribe footer to the user-provided content. +// It handles both complete HTML documents and HTML fragments. +func buildHTMLBody(body, unsubLink string) string { + footer := fmt.Sprintf( + "


If you ever wish to unsubscribe, "+ + "please click here.

", + unsubLink) + + // If body contains closing html tag, insert before it + if strings.Contains(strings.ToLower(body), "") { + return strings.Replace(body, "", footer+"", 1) + } + + // If body contains closing body tag, insert before it + if strings.Contains(strings.ToLower(body), "") { + return strings.Replace(body, "", footer+"", 1) + } + + // Otherwise just append + return body + footer } \ No newline at end of file