diff --git a/README.md b/README.md index c54d5b8fc..823a2eec6 100644 --- a/README.md +++ b/README.md @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): diff --git a/internal/mailer/template.go b/internal/mailer/template.go index ba6c22335..af47fda5d 100644 --- a/internal/mailer/template.go +++ b/internal/mailer/template.go @@ -33,6 +33,14 @@ func encodeRedirectURL(referrerURL string) string { return referrerURL } +const ( + magiclinkMailType = "magiclink" + recoveryMailType = "recovery" + inviteMailType = "invite" + signupMailType = "signup" + emailChangeMailType = "email_change" +) + const defaultInviteMail = `

You have been invited

You have been invited to create a user on {{ .SiteURL }}. Follow this link to accept the invite:

@@ -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, }) @@ -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( @@ -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 { @@ -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( @@ -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, }, ) @@ -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, @@ -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 { @@ -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( @@ -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 { @@ -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( @@ -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":