From 1c9bd53d963efb8598c6599a79e0cbfb247bed30 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Thu, 18 Aug 2022 12:30:22 -0600 Subject: [PATCH] Adding optional support for html email template errors --- accounts/routes.go | 6 ++++++ email/helper.go | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/accounts/routes.go b/accounts/routes.go index 6e518ad..0902c6c 100644 --- a/accounts/routes.go +++ b/accounts/routes.go @@ -183,6 +183,7 @@ func (s *Service) LoginEmailPOST(c echo.Context) error { "email_login_email": []string{ "email_login_email_subject.txt", "email_login_email_text.txt", + "email_login_email_html.html", }, } data := gobwebs.Map{ @@ -190,6 +191,7 @@ func (s *Service) LoginEmailPOST(c echo.Context) error { "confirmation": conf, } helper := email.NewHelper(gctx.Server.Email, tmap, tmpl) + helper.IgnoreHTMLErrors = true err = helper.Send( "email_login_email", gctx.Server.Config.DefaultFromEmail, @@ -399,6 +401,7 @@ func (s *Service) ForgotPasswordPOST(c echo.Context) error { "email_forgot_password": []string{ "email_forgot_password_subject.txt", "email_forgot_password_text.txt", + "email_forgot_password_html.html", }, } data := gobwebs.Map{ @@ -406,6 +409,7 @@ func (s *Service) ForgotPasswordPOST(c echo.Context) error { "confirmation": conf, } helper := email.NewHelper(gctx.Server.Email, tmap, tmpl) + helper.IgnoreHTMLErrors = true err = helper.Send( "email_forgot_password", gctx.Server.Config.DefaultFromEmail, @@ -716,6 +720,7 @@ func (s *Service) UpdateEmailPOST(c echo.Context) error { "email_update_email": []string{ "email_update_email_subject.txt", "email_update_email_text.txt", + "email_update_email_html.html", }, } data := gobwebs.Map{ @@ -723,6 +728,7 @@ func (s *Service) UpdateEmailPOST(c echo.Context) error { "confirmation": conf, } helper := email.NewHelper(gctx.Server.Email, tmap, tmpl) + helper.IgnoreHTMLErrors = true err = helper.Send( "email_update_email", gctx.Server.Config.DefaultFromEmail, diff --git a/email/helper.go b/email/helper.go index 37110c3..4e100bb 100644 --- a/email/helper.go +++ b/email/helper.go @@ -13,15 +13,19 @@ import ( type TMap map[string][]string // Helper is a template wrapper for quickly sending emails +// Set IgnoreHTMLErrors to true if you don't want to worry +// about errors parsing html template. If any errors exist +// the html portion will not be attached to the email. type Helper struct { - q *ServiceQueue - tmap TMap - tmpl *template.Template + q *ServiceQueue + tmap TMap + tmpl *template.Template + IgnoreHTMLErrors bool } // NewHelper returns a new email Helper func NewHelper(queue *ServiceQueue, tmap TMap, t *template.Template) *Helper { - return &Helper{queue, tmap, t} + return &Helper{queue, tmap, t, false} } // Map ... @@ -61,7 +65,11 @@ func (h *Helper) Send(name, from, rcpt string, data gobwebs.Map) error { } if htmlok { if err := h.tmpl.ExecuteTemplate(&hbody, hname, data); err != nil { - return err + if h.IgnoreHTMLErrors { + htmlok = false + } else { + return err + } } } -- 2.45.2