~netlandish/gobwebs

1c9bd53d963efb8598c6599a79e0cbfb247bed30 — Peter Sanchez 2 years ago 1f9abf8
Adding optional support for html email template errors
2 files changed, 19 insertions(+), 5 deletions(-)

M accounts/routes.go
M email/helper.go
M accounts/routes.go => accounts/routes.go +6 -0
@@ 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,

M email/helper.go => email/helper.go +13 -5
@@ 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
			}
		}
	}