1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-08 04:03:53 +02:00

Split emails to have seperate HTML and Text layouts.

This commit is contained in:
Kris Runzer 2015-03-03 11:23:49 -08:00
parent 045b9331c7
commit 7ff35efe54
7 changed files with 37 additions and 22 deletions

View File

@ -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(`<html><body>{{template "authboss" .}}</body></html>`)),
LayoutEmail: template.Must(template.New("").Parse(`<html><body>{{template "authboss" .}}</body></html>`)),
Layout: template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`)),
LayoutHTMLEmail: template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`)),
LayoutTextEmail: template.Must(template.New("").Parse(`{{template "authboss" .}}`)),
AuthLoginOKPath: "/",
AuthLoginFailPath: "/",

View File

@ -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)
}

View File

@ -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")
}
}

View File

@ -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}
}

View File

@ -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)
}

View File

@ -51,7 +51,8 @@ func init() {
type Recover struct {
templates render.Templates
emailTemplates 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)
}
}

View File

@ -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(`<strong>{{template "authboss" .}}</strong>`))
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 {