Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MailType as email template parameter #1451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -587,7 +587,7 @@ Email subject to use for email change confirmation. Defaults to `Confirm Email C
`MAILER_TEMPLATES_INVITE` - `string`

URL path to an email template to use when inviting a user. (e.g. `https://www.example.com/path-to-email-template.html`)
`SiteURL`, `Email`, and `ConfirmationURL` variables are available.
`SiteURL`, `Email`, `MailType`, and `ConfirmationURL` variables are available.

Default Content (if template is unavailable):

Expand All @@ -604,7 +604,7 @@ Default Content (if template is unavailable):
`MAILER_TEMPLATES_CONFIRMATION` - `string`

URL path to an email template to use when confirming a signup. (e.g. `https://www.example.com/path-to-email-template.html`)
`SiteURL`, `Email`, and `ConfirmationURL` variables are available.
`SiteURL`, `Email`, `MailType`, and `ConfirmationURL` variables are available.

Default Content (if template is unavailable):

Expand All @@ -618,7 +618,7 @@ Default Content (if template is unavailable):
`MAILER_TEMPLATES_RECOVERY` - `string`

URL path to an email template to use when resetting a password. (e.g. `https://www.example.com/path-to-email-template.html`)
`SiteURL`, `Email`, and `ConfirmationURL` variables are available.
`SiteURL`, `Email`, `MailType`, and `ConfirmationURL` variables are available.

Default Content (if template is unavailable):

Expand All @@ -632,7 +632,7 @@ Default Content (if template is unavailable):
`MAILER_TEMPLATES_MAGIC_LINK` - `string`

URL path to an email template to use when sending magic link. (e.g. `https://www.example.com/path-to-email-template.html`)
`SiteURL`, `Email`, and `ConfirmationURL` variables are available.
`SiteURL`, `Email`, `MailType`, and `ConfirmationURL` variables are available.

Default Content (if template is unavailable):

Expand All @@ -646,7 +646,7 @@ Default Content (if template is unavailable):
`MAILER_TEMPLATES_EMAIL_CHANGE` - `string`

URL path to an email template to use when confirming the change of an email address. (e.g. `https://www.example.com/path-to-email-template.html`)
`SiteURL`, `Email`, `NewEmail`, and `ConfirmationURL` variables are available.
`SiteURL`, `Email`, `NewEmail`, `MailType`, and `ConfirmationURL` variables are available.

Default Content (if template is unavailable):

Expand Down
33 changes: 19 additions & 14 deletions internal/mailer/template.go
Expand Up @@ -39,7 +39,7 @@ const (
RecoveryVerification = "recovery"
InviteVerification = "invite"
MagicLinkVerification = "magiclink"
EmailChangeVerification = "email_change"
EmailChangeMailType = "email_change"
EmailOTPVerification = "email"
EmailChangeCurrentVerification = "email_change_current"
EmailChangeNewVerification = "email_change_new"
Expand Down Expand Up @@ -91,7 +91,7 @@ func (m TemplateMailer) ValidateEmail(email string) error {
func (m *TemplateMailer) InviteMail(r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Invite, &EmailParams{
Token: user.ConfirmationToken,
Type: "invite",
Type: InviteVerification,
RedirectTo: referrerURL,
})

Expand All @@ -107,6 +107,7 @@ func (m *TemplateMailer) InviteMail(r *http.Request, user *models.User, otp, ref
"TokenHash": user.ConfirmationToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": InviteVerification,
}

return m.Mailer.Mail(
Expand All @@ -122,7 +123,7 @@ func (m *TemplateMailer) InviteMail(r *http.Request, user *models.User, otp, ref
func (m *TemplateMailer) ConfirmationMail(r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Confirmation, &EmailParams{
Token: user.ConfirmationToken,
Type: "signup",
Type: SignupVerification,
RedirectTo: referrerURL,
})
if err != nil {
Expand All @@ -137,6 +138,7 @@ func (m *TemplateMailer) ConfirmationMail(r *http.Request, user *models.User, ot
"TokenHash": user.ConfirmationToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": SignupVerification,
}

return m.Mailer.Mail(
Expand Down Expand Up @@ -202,7 +204,7 @@ func (m *TemplateMailer) EmailChangeMail(r *http.Request, user *models.User, otp
m.Config.Mailer.URLPaths.EmailChange,
&EmailParams{
Token: email.TokenHash,
Type: "email_change",
Type: EmailChangeMailType,
RedirectTo: referrerURL,
},
)
Expand All @@ -220,6 +222,7 @@ func (m *TemplateMailer) EmailChangeMail(r *http.Request, user *models.User, otp
"SendingTo": address,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": EmailChangeMailType,
}
errors <- m.Mailer.Mail(
address,
Expand All @@ -245,7 +248,7 @@ func (m *TemplateMailer) EmailChangeMail(r *http.Request, user *models.User, otp
func (m *TemplateMailer) RecoveryMail(r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
Token: user.RecoveryToken,
Type: "recovery",
Type: RecoveryVerification,
RedirectTo: referrerURL,
})
if err != nil {
Expand All @@ -259,6 +262,7 @@ func (m *TemplateMailer) RecoveryMail(r *http.Request, user *models.User, otp, r
"TokenHash": user.RecoveryToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": RecoveryVerification,
}

return m.Mailer.Mail(
Expand All @@ -274,7 +278,7 @@ func (m *TemplateMailer) RecoveryMail(r *http.Request, user *models.User, otp, r
func (m *TemplateMailer) MagicLinkMail(r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
Token: user.RecoveryToken,
Type: "magiclink",
Type: MagicLinkVerification,
RedirectTo: referrerURL,
})
if err != nil {
Expand All @@ -289,6 +293,7 @@ func (m *TemplateMailer) MagicLinkMail(r *http.Request, user *models.User, otp,
"TokenHash": user.RecoveryToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": MagicLinkVerification,
}

return m.Mailer.Mail(
Expand Down Expand Up @@ -317,28 +322,28 @@ func (m TemplateMailer) GetEmailActionLink(user *models.User, actionType, referr
var path *url.URL

switch actionType {
case "magiclink":
case MagicLinkVerification:
path, err = getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
Token: user.RecoveryToken,
Type: "magiclink",
Type: MagicLinkVerification,
RedirectTo: referrerURL,
})
case "recovery":
case RecoveryVerification:
path, err = getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
Token: user.RecoveryToken,
Type: "recovery",
Type: RecoveryVerification,
RedirectTo: referrerURL,
})
case "invite":
case InviteVerification:
path, err = getPath(m.Config.Mailer.URLPaths.Invite, &EmailParams{
Token: user.ConfirmationToken,
Type: "invite",
Type: InviteVerification,
RedirectTo: referrerURL,
})
case "signup":
case SignupVerification:
path, err = getPath(m.Config.Mailer.URLPaths.Confirmation, &EmailParams{
Token: user.ConfirmationToken,
Type: "signup",
Type: SignupVerification,
RedirectTo: referrerURL,
})
case "email_change_current":
Expand Down