-
Notifications
You must be signed in to change notification settings - Fork 117
/
email.go
169 lines (147 loc) · 5.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package email
import (
"bytes"
"embed"
"fmt"
"html/template"
"net/url"
)
//go:embed templates/gen/*
var templatesFS embed.FS
type Client struct {
sender Sender
frontendURL string
externalURL string
templates *template.Template
}
func New(sender Sender, frontendURL, externalURL string) *Client {
_, err := url.Parse(frontendURL)
if err != nil {
panic(fmt.Errorf("invalid frontendURL: %w", err))
}
return &Client{
sender: sender,
frontendURL: frontendURL,
externalURL: externalURL,
templates: template.Must(template.New("").ParseFS(templatesFS, "templates/gen/*.html")),
}
}
type CallToAction struct {
ToEmail string
ToName string
Subject string
Title string
Body template.HTML
ButtonText string
ButtonLink string
}
func (c *Client) SendCallToAction(opts *CallToAction) error {
buf := new(bytes.Buffer)
err := c.templates.Lookup("call_to_action.html").Execute(buf, opts)
if err != nil {
return fmt.Errorf("email template error: %w", err)
}
html := buf.String()
return c.sender.Send(opts.ToEmail, opts.ToName, opts.Subject, html)
}
type OrganizationInvite struct {
ToEmail string
ToName string
OrgName string
RoleName string
InvitedByName string
}
func (c *Client) SendOrganizationInvite(opts *OrganizationInvite) error {
if opts.InvitedByName == "" {
opts.InvitedByName = "Rill"
}
// Create link URL as "{{ admin URL }}/auth/signup?redirect={{ org frontend URL }}"
queryParams := url.Values{}
queryParams.Add("redirect", mustJoinURLPath(c.frontendURL, opts.OrgName))
finalURL := mustJoinURLPath(c.externalURL, "/auth/signup") + "?" + queryParams.Encode()
return c.SendCallToAction(&CallToAction{
ToEmail: opts.ToEmail,
ToName: opts.ToName,
Subject: fmt.Sprintf("%s invited you to join Rill", opts.InvitedByName),
Title: "Accept your invitation to Rill",
Body: template.HTML(fmt.Sprintf("%s has invited you to join <b>%s</b> as a %s for their Rill account. Get started interacting with fast, exploratory dashboards by clicking the button below to sign in and accept your invitation.", opts.InvitedByName, opts.OrgName, opts.RoleName)),
ButtonText: "Accept invitation",
ButtonLink: finalURL,
})
}
type OrganizationAddition struct {
ToEmail string
ToName string
OrgName string
RoleName string
InvitedByName string
}
func (c *Client) SendOrganizationAddition(opts *OrganizationAddition) error {
if opts.InvitedByName == "" {
opts.InvitedByName = "Rill"
}
return c.SendCallToAction(&CallToAction{
ToEmail: opts.ToEmail,
ToName: opts.ToName,
Subject: fmt.Sprintf("%s has added you to %s", opts.InvitedByName, opts.OrgName),
Title: fmt.Sprintf("%s has added you to %s", opts.InvitedByName, opts.OrgName),
Body: template.HTML(fmt.Sprintf("%s has added you as a %s for <b>%s</b>. Click the button below to view and collaborate on Rill dashboard projects for %s.", opts.InvitedByName, opts.RoleName, opts.OrgName, opts.OrgName)),
ButtonText: "View account",
ButtonLink: mustJoinURLPath(c.frontendURL, opts.OrgName),
})
}
type ProjectInvite struct {
ToEmail string
ToName string
OrgName string
ProjectName string
RoleName string
InvitedByName string
}
func (c *Client) SendProjectInvite(opts *ProjectInvite) error {
if opts.InvitedByName == "" {
opts.InvitedByName = "Rill"
}
// Create link URL as "{{ admin URL }}/auth/signup?redirect={{ project frontend URL }}"
queryParams := url.Values{}
queryParams.Add("redirect", mustJoinURLPath(c.frontendURL, opts.OrgName, opts.ProjectName))
finalURL := mustJoinURLPath(c.externalURL, "/auth/signup") + "?" + queryParams.Encode()
return c.SendCallToAction(&CallToAction{
ToEmail: opts.ToEmail,
ToName: opts.ToName,
Subject: fmt.Sprintf("You have been invited to the %s/%s project", opts.OrgName, opts.ProjectName),
Title: fmt.Sprintf("You have been invited to the %s/%s project", opts.OrgName, opts.ProjectName),
Body: template.HTML(fmt.Sprintf("%s has invited you to collaborate as a %s for the <b>%s/%s</b> project. Click the button below to accept your invitation. ", opts.InvitedByName, opts.RoleName, opts.OrgName, opts.ProjectName)),
ButtonText: "Accept invitation",
ButtonLink: finalURL,
})
}
type ProjectAddition struct {
ToEmail string
ToName string
OrgName string
ProjectName string
RoleName string
InvitedByName string
}
func (c *Client) SendProjectAddition(opts *ProjectAddition) error {
if opts.InvitedByName == "" {
opts.InvitedByName = "Rill"
}
return c.SendCallToAction(&CallToAction{
ToEmail: opts.ToEmail,
ToName: opts.ToName,
Subject: fmt.Sprintf("You have been added to the %s/%s project", opts.OrgName, opts.ProjectName),
Title: fmt.Sprintf("You have been added to the %s/%s project", opts.OrgName, opts.ProjectName),
Body: template.HTML(fmt.Sprintf("%s has invited you to collaborate as a %s for the <b>%s</b> project. Click the button below to accept your invitation. ", opts.InvitedByName, opts.RoleName, opts.ProjectName)),
ButtonText: "View account",
ButtonLink: mustJoinURLPath(c.frontendURL, opts.OrgName, opts.ProjectName),
})
}
func mustJoinURLPath(base string, elem ...string) string {
res, err := url.JoinPath(base, elem...)
if err != nil {
panic(err)
}
return res
}