Skip to content

Commit 77fc17e

Browse files
committed
MailType as email template parameter
1 parent f29e89d commit 77fc17e

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ Email subject to use for email change confirmation. Defaults to `Confirm Email C
587587
`MAILER_TEMPLATES_INVITE` - `string`
588588

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

592592
Default Content (if template is unavailable):
593593

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

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

609609
Default Content (if template is unavailable):
610610

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

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

623623
Default Content (if template is unavailable):
624624

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

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

637637
Default Content (if template is unavailable):
638638

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

648648
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`)
649-
`SiteURL`, `Email`, `NewEmail`, and `ConfirmationURL` variables are available.
649+
`SiteURL`, `Email`, `NewEmail`, `MailType`, and `ConfirmationURL` variables are available.
650650

651651
Default Content (if template is unavailable):
652652

internal/mailer/template.go

+26-13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ func encodeRedirectURL(referrerURL string) string {
3333
return referrerURL
3434
}
3535

36+
const (
37+
magiclinkMailType = "magiclink"
38+
recoveryMailType = "recovery"
39+
inviteMailType = "invite"
40+
signupMailType = "signup"
41+
emailChangeMailType = "email_change"
42+
)
43+
3644
const defaultInviteMail = `<h2>You have been invited</h2>
3745
3846
<p>You have been invited to create a user on {{ .SiteURL }}. Follow this link to accept the invite:</p>
@@ -78,7 +86,7 @@ func (m TemplateMailer) ValidateEmail(email string) error {
7886
func (m *TemplateMailer) InviteMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
7987
path, err := getPath(m.Config.Mailer.URLPaths.Invite, &EmailParams{
8088
Token: user.ConfirmationToken,
81-
Type: "invite",
89+
Type: inviteMailType,
8290
RedirectTo: referrerURL,
8391
})
8492

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

99108
return m.Mailer.Mail(
@@ -109,7 +118,7 @@ func (m *TemplateMailer) InviteMail(user *models.User, otp, referrerURL string,
109118
func (m *TemplateMailer) ConfirmationMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
110119
path, err := getPath(m.Config.Mailer.URLPaths.Confirmation, &EmailParams{
111120
Token: user.ConfirmationToken,
112-
Type: "signup",
121+
Type: signupMailType,
113122
RedirectTo: referrerURL,
114123
})
115124
if err != nil {
@@ -124,6 +133,7 @@ func (m *TemplateMailer) ConfirmationMail(user *models.User, otp, referrerURL st
124133
"TokenHash": user.ConfirmationToken,
125134
"Data": user.UserMetaData,
126135
"RedirectTo": referrerURL,
136+
"MailType": signupMailType,
127137
}
128138

129139
return m.Mailer.Mail(
@@ -189,7 +199,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
189199
m.Config.Mailer.URLPaths.EmailChange,
190200
&EmailParams{
191201
Token: email.TokenHash,
192-
Type: "email_change",
202+
Type: emailChangeMailType,
193203
RedirectTo: referrerURL,
194204
},
195205
)
@@ -207,6 +217,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
207217
"SendingTo": address,
208218
"Data": user.UserMetaData,
209219
"RedirectTo": referrerURL,
220+
"MailType": emailChangeMailType,
210221
}
211222
errors <- m.Mailer.Mail(
212223
address,
@@ -232,7 +243,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
232243
func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
233244
path, err := getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
234245
Token: user.RecoveryToken,
235-
Type: "recovery",
246+
Type: recoveryMailType,
236247
RedirectTo: referrerURL,
237248
})
238249
if err != nil {
@@ -246,6 +257,7 @@ func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string
246257
"TokenHash": user.RecoveryToken,
247258
"Data": user.UserMetaData,
248259
"RedirectTo": referrerURL,
260+
"MailType": recoveryMailType,
249261
}
250262

251263
return m.Mailer.Mail(
@@ -261,7 +273,7 @@ func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string
261273
func (m *TemplateMailer) MagicLinkMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
262274
path, err := getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
263275
Token: user.RecoveryToken,
264-
Type: "magiclink",
276+
Type: magiclinkMailType,
265277
RedirectTo: referrerURL,
266278
})
267279
if err != nil {
@@ -276,6 +288,7 @@ func (m *TemplateMailer) MagicLinkMail(user *models.User, otp, referrerURL strin
276288
"TokenHash": user.RecoveryToken,
277289
"Data": user.UserMetaData,
278290
"RedirectTo": referrerURL,
291+
"MailType": magiclinkMailType,
279292
}
280293

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

306319
switch actionType {
307-
case "magiclink":
320+
case magiclinkMailType:
308321
path, err = getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
309322
Token: user.RecoveryToken,
310-
Type: "magiclink",
323+
Type: magiclinkMailType,
311324
RedirectTo: referrerURL,
312325
})
313-
case "recovery":
326+
case recoveryMailType:
314327
path, err = getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
315328
Token: user.RecoveryToken,
316-
Type: "recovery",
329+
Type: recoveryMailType,
317330
RedirectTo: referrerURL,
318331
})
319-
case "invite":
332+
case inviteMailType:
320333
path, err = getPath(m.Config.Mailer.URLPaths.Invite, &EmailParams{
321334
Token: user.ConfirmationToken,
322-
Type: "invite",
335+
Type: inviteMailType,
323336
RedirectTo: referrerURL,
324337
})
325-
case "signup":
338+
case signupMailType:
326339
path, err = getPath(m.Config.Mailer.URLPaths.Confirmation, &EmailParams{
327340
Token: user.ConfirmationToken,
328-
Type: "signup",
341+
Type: signupMailType,
329342
RedirectTo: referrerURL,
330343
})
331344
case "email_change_current":

0 commit comments

Comments
 (0)