From f49c0b1c4c60f6157bc144a2e96a29089e119ee1 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 24 Oct 2024 16:13:36 -0400 Subject: [PATCH] Send password resets and login emails with new email layer (Untested) Ref T905 T731 --- account.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/account.go b/account.go index 423dee2..363af62 100644 --- a/account.go +++ b/account.go @@ -13,7 +13,7 @@ package writefreely import ( "encoding/json" "fmt" - "github.com/mailgun/mailgun-go" + "github.com/writefreely/writefreely/mailer" "github.com/writefreely/writefreely/spam" "html/template" "net/http" @@ -1378,13 +1378,19 @@ func handleResetPasswordInit(app *App, w http.ResponseWriter, r *http.Request) e func emailPasswordReset(app *App, toEmail, token string) error { // Send email - gun := mailgun.NewMailgun(app.cfg.Email.Domain, app.cfg.Email.MailgunPrivate) + mlr, err := mailer.New(app.cfg.Email) + if err != nil { + return err + } footerPara := "Didn't request this password reset? Your account is still safe, and you can safely ignore this email." plainMsg := fmt.Sprintf("We received a request to reset your password on %s. Please click the following link to continue (or copy and paste it into your browser): %s/reset?t=%s\n\n%s", app.cfg.App.SiteName, app.cfg.App.Host, token, footerPara) - m := mailgun.NewMessage(app.cfg.App.SiteName+" ", "Reset Your "+app.cfg.App.SiteName+" Password", plainMsg, fmt.Sprintf("<%s>", toEmail)) + m, err := mlr.NewMessage(app.cfg.App.SiteName+" ", "Reset Your "+app.cfg.App.SiteName+" Password", plainMsg, fmt.Sprintf("<%s>", toEmail)) + if err != nil { + return err + } m.AddTag("Password Reset") - m.SetHtml(fmt.Sprintf(` + m.SetHTML(fmt.Sprintf(`

%s

@@ -1394,8 +1400,7 @@ func emailPasswordReset(app *App, toEmail, token string) error {
`, app.cfg.App.Host, app.cfg.App.SiteName, app.cfg.App.SiteName, app.cfg.App.Host, token, footerPara)) - _, _, err := gun.Send(m) - return err + return mlr.Send(m) } func loginViaEmail(app *App, alias, redirectTo string) error { @@ -1424,15 +1429,21 @@ func loginViaEmail(app *App, alias, redirectTo string) error { } // Send email - gun := mailgun.NewMailgun(app.cfg.Email.Domain, app.cfg.Email.MailgunPrivate) + mlr, err := mailer.New(app.cfg.Email) + if err != nil { + return err + } toEmail := u.EmailClear(app.keys) footerPara := "This link will only work once and expires in 15 minutes. Didn't ask us to log in? You can safely ignore this email." plainMsg := fmt.Sprintf("Log in to %s here: %s/login?to=%s&with=%s\n\n%s", app.cfg.App.SiteName, app.cfg.App.Host, redirectTo, t, footerPara) - m := mailgun.NewMessage(app.cfg.App.SiteName+" ", "Log in to "+app.cfg.App.SiteName, plainMsg, fmt.Sprintf("<%s>", toEmail)) + m, err := mlr.NewMessage(app.cfg.App.SiteName+" ", "Log in to "+app.cfg.App.SiteName, plainMsg, fmt.Sprintf("<%s>", toEmail)) + if err != nil { + return err + } m.AddTag("Email Login") - m.SetHtml(fmt.Sprintf(` + m.SetHTML(fmt.Sprintf(`

%s

@@ -1441,9 +1452,7 @@ func loginViaEmail(app *App, alias, redirectTo string) error {
`, app.cfg.App.Host, app.cfg.App.SiteName, app.cfg.App.Host, redirectTo, t, app.cfg.App.SiteName, footerPara)) - _, _, err = gun.Send(m) - - return err + return mlr.Send(m) } func saveTempInfo(app *App, key, val string, r *http.Request, w http.ResponseWriter) error {