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 1 commit
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
39 changes: 26 additions & 13 deletions internal/mailer/template.go
Expand Up @@ -33,6 +33,14 @@ func encodeRedirectURL(referrerURL string) string {
return referrerURL
}

const (
magiclinkMailType = "magiclink"
recoveryMailType = "recovery"
inviteMailType = "invite"
signupMailType = "signup"
emailChangeMailType = "email_change"
ryanhalliday marked this conversation as resolved.
Show resolved Hide resolved
)

const defaultInviteMail = `<h2>You have been invited</h2>

<p>You have been invited to create a user on {{ .SiteURL }}. Follow this link to accept the invite:</p>
Expand Down Expand Up @@ -78,7 +86,7 @@ func (m TemplateMailer) ValidateEmail(email string) error {
func (m *TemplateMailer) InviteMail(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: inviteMailType,
RedirectTo: referrerURL,
})

Expand All @@ -94,6 +102,7 @@ func (m *TemplateMailer) InviteMail(user *models.User, otp, referrerURL string,
"TokenHash": user.ConfirmationToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": inviteMailType,
}

return m.Mailer.Mail(
Expand All @@ -109,7 +118,7 @@ func (m *TemplateMailer) InviteMail(user *models.User, otp, referrerURL string,
func (m *TemplateMailer) ConfirmationMail(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: signupMailType,
RedirectTo: referrerURL,
})
if err != nil {
Expand All @@ -124,6 +133,7 @@ func (m *TemplateMailer) ConfirmationMail(user *models.User, otp, referrerURL st
"TokenHash": user.ConfirmationToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": signupMailType,
}

return m.Mailer.Mail(
Expand Down Expand Up @@ -189,7 +199,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
m.Config.Mailer.URLPaths.EmailChange,
&EmailParams{
Token: email.TokenHash,
Type: "email_change",
Type: emailChangeMailType,
RedirectTo: referrerURL,
},
)
Expand All @@ -207,6 +217,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
"SendingTo": address,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": emailChangeMailType,
}
errors <- m.Mailer.Mail(
address,
Expand All @@ -232,7 +243,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
func (m *TemplateMailer) RecoveryMail(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: recoveryMailType,
RedirectTo: referrerURL,
})
if err != nil {
Expand All @@ -246,6 +257,7 @@ func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string
"TokenHash": user.RecoveryToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": recoveryMailType,
}

return m.Mailer.Mail(
Expand All @@ -261,7 +273,7 @@ func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string
func (m *TemplateMailer) MagicLinkMail(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: magiclinkMailType,
RedirectTo: referrerURL,
})
if err != nil {
Expand All @@ -276,6 +288,7 @@ func (m *TemplateMailer) MagicLinkMail(user *models.User, otp, referrerURL strin
"TokenHash": user.RecoveryToken,
"Data": user.UserMetaData,
"RedirectTo": referrerURL,
"MailType": magiclinkMailType,
}

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

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