1
1
package mailer
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"net/http"
6
7
"net/url"
7
8
"strings"
8
9
9
- "github.com/badoux/checkmail"
10
10
"github.com/supabase/auth/internal/conf"
11
11
"github.com/supabase/auth/internal/models"
12
12
)
13
13
14
+ type MailRequest struct {
15
+ To string
16
+ SubjectTemplate string
17
+ TemplateURL string
18
+ DefaultTemplate string
19
+ TemplateData map [string ]interface {}
20
+ Headers map [string ][]string
21
+ Type string
22
+ }
23
+
14
24
type MailClient interface {
15
- Mail (string , string , string , string , map [string ]interface {}, map [string ][]string , string ) error
25
+ Mail (
26
+ ctx context.Context ,
27
+ to string ,
28
+ subjectTemplate string ,
29
+ templateURL string ,
30
+ defaultTemplate string ,
31
+ templateData map [string ]interface {},
32
+ headers map [string ][]string ,
33
+ typ string ,
34
+ ) error
16
35
}
17
36
18
37
// TemplateMailer will send mail and use templates from the site for easy mail styling
@@ -81,12 +100,6 @@ const defaultReauthenticateMail = `<h2>Confirm reauthentication</h2>
81
100
82
101
<p>Enter the code: {{ .Token }}</p>`
83
102
84
- // ValidateEmail returns nil if the email is valid,
85
- // otherwise an error indicating the reason it is invalid
86
- func (m TemplateMailer ) ValidateEmail (email string ) error {
87
- return checkmail .ValidateFormat (email )
88
- }
89
-
90
103
func (m * TemplateMailer ) Headers (messageType string ) map [string ][]string {
91
104
originalHeaders := m .Config .SMTP .NormalizedHeaders ()
92
105
@@ -145,6 +158,7 @@ func (m *TemplateMailer) InviteMail(r *http.Request, user *models.User, otp, ref
145
158
}
146
159
147
160
return m .Mailer .Mail (
161
+ r .Context (),
148
162
user .GetEmail (),
149
163
withDefault (m .Config .Mailer .Subjects .Invite , "You have been invited" ),
150
164
m .Config .Mailer .Templates .Invite ,
@@ -177,6 +191,7 @@ func (m *TemplateMailer) ConfirmationMail(r *http.Request, user *models.User, ot
177
191
}
178
192
179
193
return m .Mailer .Mail (
194
+ r .Context (),
180
195
user .GetEmail (),
181
196
withDefault (m .Config .Mailer .Subjects .Confirmation , "Confirm Your Email" ),
182
197
m .Config .Mailer .Templates .Confirmation ,
@@ -197,6 +212,7 @@ func (m *TemplateMailer) ReauthenticateMail(r *http.Request, user *models.User,
197
212
}
198
213
199
214
return m .Mailer .Mail (
215
+ r .Context (),
200
216
user .GetEmail (),
201
217
withDefault (m .Config .Mailer .Subjects .Reauthentication , "Confirm reauthentication" ),
202
218
m .Config .Mailer .Templates .Reauthentication ,
@@ -237,7 +253,10 @@ func (m *TemplateMailer) EmailChangeMail(r *http.Request, user *models.User, otp
237
253
})
238
254
}
239
255
240
- errors := make (chan error )
256
+ ctx , cancel := context .WithCancel (r .Context ())
257
+ defer cancel ()
258
+
259
+ errors := make (chan error , len (emails ))
241
260
for _ , email := range emails {
242
261
path , err := getPath (
243
262
m .Config .Mailer .URLPaths .EmailChange ,
@@ -263,6 +282,7 @@ func (m *TemplateMailer) EmailChangeMail(r *http.Request, user *models.User, otp
263
282
"RedirectTo" : referrerURL ,
264
283
}
265
284
errors <- m .Mailer .Mail (
285
+ ctx ,
266
286
address ,
267
287
withDefault (m .Config .Mailer .Subjects .EmailChange , "Confirm Email Change" ),
268
288
template ,
@@ -280,7 +300,6 @@ func (m *TemplateMailer) EmailChangeMail(r *http.Request, user *models.User, otp
280
300
return e
281
301
}
282
302
}
283
-
284
303
return nil
285
304
}
286
305
@@ -305,6 +324,7 @@ func (m *TemplateMailer) RecoveryMail(r *http.Request, user *models.User, otp, r
305
324
}
306
325
307
326
return m .Mailer .Mail (
327
+ r .Context (),
308
328
user .GetEmail (),
309
329
withDefault (m .Config .Mailer .Subjects .Recovery , "Reset Your Password" ),
310
330
m .Config .Mailer .Templates .Recovery ,
@@ -337,6 +357,7 @@ func (m *TemplateMailer) MagicLinkMail(r *http.Request, user *models.User, otp,
337
357
}
338
358
339
359
return m .Mailer .Mail (
360
+ r .Context (),
340
361
user .GetEmail (),
341
362
withDefault (m .Config .Mailer .Subjects .MagicLink , "Your Magic Link" ),
342
363
m .Config .Mailer .Templates .MagicLink ,
@@ -347,19 +368,6 @@ func (m *TemplateMailer) MagicLinkMail(r *http.Request, user *models.User, otp,
347
368
)
348
369
}
349
370
350
- // Send can be used to send one-off emails to users
351
- func (m TemplateMailer ) Send (user * models.User , subject , body string , data map [string ]interface {}) error {
352
- return m .Mailer .Mail (
353
- user .GetEmail (),
354
- subject ,
355
- "" ,
356
- body ,
357
- data ,
358
- m .Headers ("other" ),
359
- "other" ,
360
- )
361
- }
362
-
363
371
// GetEmailActionLink returns a magiclink, recovery or invite link based on the actionType passed.
364
372
func (m TemplateMailer ) GetEmailActionLink (user * models.User , actionType , referrerURL string , externalURL * url.URL ) (string , error ) {
365
373
var err error
0 commit comments