-
Notifications
You must be signed in to change notification settings - Fork 301
/
email.go
147 lines (138 loc) · 4.49 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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package identityserver
import (
"context"
"go.thethings.network/lorawan-stack/v3/pkg/email"
"go.thethings.network/lorawan-stack/v3/pkg/email/sendgrid"
"go.thethings.network/lorawan-stack/v3/pkg/email/smtp"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/emails"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
func (is *IdentityServer) initEmailTemplates(ctx context.Context) (*email.TemplateRegistry, error) {
config := is.configFromContext(ctx).Email.Templates
fetcher, err := config.Fetcher(ctx, is.Component.GetBaseConfig(ctx).Blob, is)
if err != nil {
return nil, err
}
return email.NewTemplateRegistry(fetcher, config.Includes...)
}
// SendEmail sends an email.
func (is *IdentityServer) SendEmail(ctx context.Context, f func(emails.Data) email.MessageData) (err error) {
isConfig := is.configFromContext(ctx)
var sender email.Sender
switch isConfig.Email.Provider {
case "sendgrid":
sender, err = sendgrid.New(ctx, isConfig.Email.Config, isConfig.Email.SendGrid)
case "smtp":
sender, err = smtp.New(ctx, isConfig.Email.Config, isConfig.Email.SMTP)
}
if err != nil {
return err
}
var data emails.Data
data.Network.Name = isConfig.Email.Network.Name
data.Network.IdentityServerURL = isConfig.Email.Network.IdentityServerURL
data.Network.ConsoleURL = isConfig.Email.Network.ConsoleURL
messageData := f(data)
if messageData == nil {
return nil
}
message, err := is.emailTemplates.Render(messageData)
if err != nil {
return err
}
if sender == nil {
log.FromContext(ctx).WithFields(log.Fields(
"to", message.RecipientAddress,
"subject", message.Subject,
"body", message.TextBody,
)).Warn("Could not send email without email provider")
return nil
}
return sender.Send(message)
}
// SendUserEmail sends an email to the given user.
func (is *IdentityServer) SendUserEmail(ctx context.Context, userIDs *ttnpb.UserIdentifiers, makeMessage func(emails.Data) email.MessageData) error {
var usr *ttnpb.User
err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) {
usr, err = st.GetUser(ctx, userIDs, []string{"name", "primary_email_address"})
if err != nil {
return err
}
return err
})
if err != nil {
return err
}
err = is.SendEmail(ctx, func(data emails.Data) email.MessageData {
data.SetUser(usr)
return makeMessage(data)
})
if err != nil {
return err
}
return nil
}
// SendAdminsEmail sends an email to the admins of the network.
func (is *IdentityServer) SendAdminsEmail(ctx context.Context, makeMessage func(emails.Data) email.MessageData) error {
var users []*ttnpb.User
err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) {
users, err = st.ListAdmins(ctx, []string{"name", "primary_email_address"})
if err != nil {
return err
}
return err
})
if err != nil {
return err
}
for _, usr := range users {
err = is.SendEmail(ctx, func(data emails.Data) email.MessageData {
data.SetUser(usr)
return makeMessage(data)
})
if err != nil {
return err
}
}
return nil
}
// SendContactsEmail sends an email to the contacts of the given entity.
func (is *IdentityServer) SendContactsEmail(ctx context.Context, ids ttnpb.IDStringer, makeMessage func(emails.Data) email.MessageData) error {
var contacts []*ttnpb.ContactInfo
err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) {
contacts, err = st.GetContactInfo(ctx, ids)
return err
})
if err != nil {
return err
}
for _, contactInfo := range contacts {
if contactInfo.ContactMethod != ttnpb.ContactMethod_CONTACT_METHOD_EMAIL {
continue
}
err := is.SendEmail(ctx, func(data emails.Data) email.MessageData {
data.SetEntity(ids)
data.SetContact(contactInfo)
return makeMessage(data)
})
if err != nil {
return err
}
}
return nil
}