diff --git a/config.go b/config.go index 59c30de..45f5221 100644 --- a/config.go +++ b/config.go @@ -30,7 +30,8 @@ type Config struct { PrimaryID string Layout *template.Template - LayoutEmail *template.Template + LayoutHTMLEmail *template.Template + LayoutTextEmail *template.Template LayoutDataMaker ViewDataMaker // ErrorHandler handles would be 500 errors. @@ -81,8 +82,9 @@ func NewConfig() *Config { PrimaryID: StoreEmail, - Layout: template.Must(template.New("").Parse(`{{template "authboss" .}}`)), - LayoutEmail: template.Must(template.New("").Parse(`{{template "authboss" .}}`)), + Layout: template.Must(template.New("").Parse(`{{template "authboss" .}}`)), + LayoutHTMLEmail: template.Must(template.New("").Parse(`{{template "authboss" .}}`)), + LayoutTextEmail: template.Must(template.New("").Parse(`{{template "authboss" .}}`)), AuthLoginOKPath: "/", AuthLoginFailPath: "/", diff --git a/confirm/confirm.go b/confirm/confirm.go index af2607c..6d47f86 100644 --- a/confirm/confirm.go +++ b/confirm/confirm.go @@ -44,7 +44,8 @@ func init() { } type Confirm struct { - emailTemplates render.Templates + emailHTMLTemplates render.Templates + emailTextTemplates render.Templates } func (c *Confirm) Initialize() (err error) { @@ -54,7 +55,11 @@ func (c *Confirm) Initialize() (err error) { return errors.New("confirm: Need a ConfirmStorer.") } - c.emailTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutEmail, authboss.Cfg.ViewsPath, tplConfirmHTML, tplConfirmText) + c.emailHTMLTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutHTMLEmail, authboss.Cfg.ViewsPath, tplConfirmHTML) + if err != nil { + return err + } + c.emailTextTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutTextEmail, authboss.Cfg.ViewsPath, tplConfirmText) if err != nil { return err } @@ -132,7 +137,7 @@ func (c *Confirm) confirmEmail(to, token string) { Subject: authboss.Cfg.EmailSubjectPrefix + "Confirm New Account", } - err := c.emailTemplates.RenderEmail(email, tplConfirmHTML, tplConfirmText, url) + err := render.RenderEmail(email, c.emailHTMLTemplates, tplConfirmHTML, c.emailTextTemplates, tplConfirmText, url) if err != nil { fmt.Fprintf(authboss.Cfg.LogWriter, "confirm: Failed to send e-mail: %v", err) } diff --git a/confirm/confirm_test.go b/confirm/confirm_test.go index b5ae1fc..a98a939 100644 --- a/confirm/confirm_test.go +++ b/confirm/confirm_test.go @@ -19,7 +19,8 @@ import ( func setup() *Confirm { authboss.NewConfig() authboss.Cfg.Storer = mocks.NewMockStorer() - authboss.Cfg.LayoutEmail = template.Must(template.New("").Parse(`email ^_^`)) + authboss.Cfg.LayoutHTMLEmail = template.Must(template.New("").Parse(`email ^_^`)) + authboss.Cfg.LayoutTextEmail = template.Must(template.New("").Parse(`email`)) c := &Confirm{} if err := c.Initialize(); err != nil { @@ -37,8 +38,11 @@ func TestConfirm_Initialize(t *testing.T) { c = setup() - if c.emailTemplates == nil { - t.Error("Missing email templates") + if c.emailHTMLTemplates == nil { + t.Error("Missing HTML email templates") + } + if c.emailTextTemplates == nil { + t.Error("Missing text email templates") } } diff --git a/internal/render/render.go b/internal/render/render.go index d6d14e1..0f08c93 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -107,13 +107,13 @@ func (t Templates) Render(ctx *authboss.Context, w http.ResponseWriter, r *http. } // RenderEmail renders the html and plaintext views for an email and sends it -func (t Templates) RenderEmail(email authboss.Email, nameHTML, namePlain string, data interface{}) error { - tplHTML, ok := t[nameHTML] +func RenderEmail(email authboss.Email, htmlTpls Templates, nameHTML string, textTpls Templates, namePlain string, data interface{}) error { + tplHTML, ok := htmlTpls[nameHTML] if !ok { return authboss.RenderErr{tplHTML.Name(), data, ErrTemplateNotFound} } - tplPlain, ok := t[namePlain] + tplPlain, ok := textTpls[namePlain] if !ok { return authboss.RenderErr{tplPlain.Name(), data, ErrTemplateNotFound} } diff --git a/internal/render/render_test.go b/internal/render/render_test.go index da0a68a..395a3ee 100644 --- a/internal/render/render_test.go +++ b/internal/render/render_test.go @@ -88,16 +88,14 @@ func TestTemplates_RenderEmail(t *testing.T) { mockMailer := &mocks.MockMailer{} authboss.Cfg.Mailer = mockMailer - tpls := Templates{ - "html": testEmailHTMLTempalte, - "plain": testEmailPlainTempalte, - } + htmlTpls := Templates{"html": testEmailHTMLTempalte} + textTpls := Templates{"plain": testEmailPlainTempalte} email := authboss.Email{ To: []string{"a@b.c"}, } - err := tpls.RenderEmail(email, "html", "plain", "spoon") + err := RenderEmail(email, htmlTpls, "html", textTpls, "plain", "spoon") if err != nil { t.Error(err) } diff --git a/recover/recover.go b/recover/recover.go index ac1c853..5075cae 100644 --- a/recover/recover.go +++ b/recover/recover.go @@ -50,8 +50,9 @@ func init() { } type Recover struct { - templates render.Templates - emailTemplates render.Templates + templates render.Templates + emailHTMLTemplates render.Templates + emailTextTemplates render.Templates } func (r *Recover) Initialize() (err error) { @@ -76,7 +77,11 @@ func (r *Recover) Initialize() (err error) { return err } - r.emailTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutEmail, authboss.Cfg.ViewsPath, tplInitHTMLEmail, tplInitTextEmail) + r.emailHTMLTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutHTMLEmail, authboss.Cfg.ViewsPath, tplInitHTMLEmail) + if err != nil { + return err + } + r.emailTextTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutTextEmail, authboss.Cfg.ViewsPath, tplInitTextEmail) if err != nil { return err } @@ -184,7 +189,7 @@ func (r *Recover) sendRecoverEmail(to, encodedToken string) { Subject: authboss.Cfg.EmailSubjectPrefix + "Password Reset", } - if err := r.emailTemplates.RenderEmail(email, tplInitHTMLEmail, tplInitTextEmail, url); err != nil { + if err := render.RenderEmail(email, r.emailHTMLTemplates, tplInitHTMLEmail, r.emailTextTemplates, tplInitTextEmail, url); err != nil { fmt.Fprintln(authboss.Cfg.LogWriter, "recover: failed to send recover email:", err) } } diff --git a/recover/recover_test.go b/recover/recover_test.go index 932bd1d..b8375fb 100644 --- a/recover/recover_test.go +++ b/recover/recover_test.go @@ -26,7 +26,8 @@ func testSetup() (r *Recover, s *mocks.MockStorer, l *bytes.Buffer) { authboss.Cfg = authboss.NewConfig() authboss.Cfg.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) - authboss.Cfg.LayoutEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) + authboss.Cfg.LayoutHTMLEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) + authboss.Cfg.LayoutTextEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) authboss.Cfg.Storer = s authboss.Cfg.XSRFName = "xsrf" authboss.Cfg.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string {