-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
151 lines (119 loc) · 3.77 KB
/
email.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package emailtemplates
import (
"net/url"
"github.com/theopenlane/newman"
)
// NewVerifyEmail returns a new email message based on the config values and the provided recipient and token
func (c Config) NewVerifyEmail(r Recipient, token string) (*newman.EmailMessage, error) {
data := VerifyEmailData{
EmailData: EmailData{
Config: c,
Recipient: r,
},
}
var err error
data.URLS.Verify, err = addTokenToURL(c.URLS.Verify, token)
if err != nil {
return nil, err
}
return verify(data)
}
// NewWelcomeEmail returns a new email message based on the config values and the provided recipient and organization name
func (c Config) NewWelcomeEmail(r Recipient, org string) (*newman.EmailMessage, error) {
data := WelcomeData{
EmailData: EmailData{
Config: c,
Recipient: r,
},
Organization: org,
}
return welcome(data)
}
// InviteTemplateData includes the data needed to render the invite email templates
type InviteTemplateData struct {
InviterName string
OrganizationName string
Role string
}
// NewInviteEmail returns a new email message based on the config values and the provided recipient and invite data
func (c Config) NewInviteEmail(r Recipient, i InviteTemplateData, token string) (*newman.EmailMessage, error) {
data := c.newInvite(r, i)
data.Recipient = r
var err error
data.URLS.Invite, err = addTokenToURL(c.URLS.Invite, token)
if err != nil {
return nil, err
}
return invite(data)
}
// NewInviteEmail returns a new email message based on the config values and the provided recipient and invite data
func (c Config) NewInviteAcceptedEmail(r Recipient, i InviteTemplateData) (*newman.EmailMessage, error) {
data := c.newInvite(r, i)
return inviteAccepted(data)
}
// newInvite creates new invite data for use in the invite emails
func (c Config) newInvite(r Recipient, i InviteTemplateData) InviteData {
data := InviteData{
EmailData: EmailData{
Config: c,
Recipient: r,
},
InviterName: i.InviterName,
OrganizationName: i.OrganizationName,
Role: i.Role,
}
return data
}
// NewPasswordResetRequestEmail returns a new email message based on the config values and the provided recipient and token
func (c Config) NewPasswordResetRequestEmail(r Recipient, token string) (*newman.EmailMessage, error) {
data := ResetRequestData{
EmailData: EmailData{
Config: c,
Recipient: r,
},
}
var err error
data.URLS.PasswordReset, err = addTokenToURL(c.URLS.PasswordReset, token)
if err != nil {
return nil, err
}
return passwordResetRequest(data)
}
// NewPasswordResetSuccessEmail returns a new email message based on the config values and the provided recipient
func (c Config) NewPasswordResetSuccessEmail(r Recipient) (*newman.EmailMessage, error) {
data := ResetSuccessData{
EmailData: EmailData{
Config: c,
Recipient: r,
},
}
return passwordResetSuccess(data)
}
// NewSubscriberEmail returns a new email message based on the config values and the provided recipient, organization name, and token
func (c Config) NewSubscriberEmail(r Recipient, organizationName, token string) (*newman.EmailMessage, error) {
data := SubscriberEmailData{
EmailData: EmailData{
Config: c,
Recipient: r,
},
OrganizationName: organizationName,
}
var err error
data.URLS.VerifySubscriber, err = addTokenToURL(c.URLS.VerifySubscriber, token)
if err != nil {
return nil, err
}
return subscribe(data)
}
// addTokenToURL adds a token to the URL as a query parameter
func addTokenToURL(baseURL, token string) (string, error) {
if token == "" {
return "", newMissingRequiredFieldError("token")
}
base, err := url.Parse(baseURL)
if err != nil {
return "", err
}
url := base.ResolveReference(&url.URL{RawQuery: url.Values{"token": []string{token}}.Encode()})
return url.String(), nil
}