onasty/mailer/template.go(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package main
import (
"errors"
"fmt"
)
var ErrInvalidTemplate = errors.New("failed to get template")
type Template struct {
Subject string
Body string
}
type TemplateFunc func(args map[string]string) Template
func getTemplate(appURL, frontendURL string, templateName string) (TemplateFunc, error) {
switch templateName {
case "email_verification":
return emailVerificationTemplate(appURL), nil
case "reset_password":
return passwordResetTemplate(frontendURL), nil
case "confirm_email_change":
return confirmEmailChangeTemplate(appURL), nil
default:
return nil, ErrInvalidTemplate
}
}
func emailVerificationTemplate(appURL string) TemplateFunc {
return func(opts map[string]string) Template {
link := fmt.Sprintf("%[1]s/api/v1/auth/verify/%[2]s", appURL, opts["token"])
return Template{
Subject: "Onasty: verify your email",
Body: fmt.Sprintf(`To verify your email, please follow this link:
<a href="%[1]s">%[1]s</a>
<br>
<br>
This link will expire after 24 hours.`, link),
}
}
}
func passwordResetTemplate(frontendURL string) TemplateFunc {
return func(opts map[string]string) Template {
link := fmt.Sprintf("%[1]s/auth?token=%[2]s", frontendURL, opts["token"])
return Template{
Subject: "Onasty: reset your password",
Body: fmt.Sprintf(`To reset your password, use this api:
<a href="%[1]s">%[1]s</a>
<br>
<br>
This link will expire after an hour.`, link),
}
}
}
func confirmEmailChangeTemplate(appURL string) TemplateFunc {
return func(opts map[string]string) Template {
link := fmt.Sprintf("%[1]s/api/v1/auth/change-email/%[2]s", appURL, opts["token"])
return Template{
Subject: "Onasty: confirm your email change",
Body: fmt.Sprintf(`
It seems like you have changed your email address to %[1]s.
<br>
To confirm this change, please follow this link:
<a href="%[2]s">%[2]s</a>
<br>
<br>
If you did not request email change, you can ignore this message.
<br>
This link will expire after 24 hours.
`, opts["email"], link),
}
}
}
|