-
Notifications
You must be signed in to change notification settings - Fork 12
/
domainEmail.go
111 lines (92 loc) · 3.05 KB
/
domainEmail.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
package service
import (
"github.com/EmissarySocial/emissary/config"
"github.com/EmissarySocial/emissary/model"
"github.com/EmissarySocial/emissary/tools/domain"
"github.com/benpate/derp"
"github.com/benpate/rosetta/mapof"
)
type DomainEmail struct {
serverEmail *ServerEmail
smtp config.SMTPConnection
owner config.Owner
label string
hostname string
}
func NewDomainEmail(serverEmail *ServerEmail, configuration config.Domain) DomainEmail {
service := DomainEmail{
serverEmail: serverEmail,
smtp: configuration.SMTPConnection,
owner: configuration.Owner,
label: configuration.Label,
hostname: configuration.Hostname,
}
service.Refresh(configuration)
return service
}
/******************************************
* Lifecycle Methods
******************************************/
func (service *DomainEmail) Refresh(configuration config.Domain) {
// Add configuration to the service
service.label = configuration.Label
service.hostname = configuration.Hostname
service.smtp = configuration.SMTPConnection
service.owner = configuration.Owner
}
/*******************************
* HARD-CODED EMAIL TEMPLATES
*******************************/
// SendWelcome sends a welcome email to the user. This method
// returns an error so that it CAN NOT be run asynchronously.
func (service *DomainEmail) SendWelcome(user *model.User) error {
// Send the welcome email
err := service.serverEmail.Send(
service.smtp,
"user-welcome",
service.owner.EmailAddress,
[]string{user.EmailAddress},
"Welcome to Emissary",
mapof.Any{
// User info available to the template
"UserID": user.UserID.Hex(),
"Username": user.Username,
"DisplayName": user.DisplayName,
"ResetCode": user.PasswordReset.AuthCode,
"ExpireDate": user.PasswordReset.ExpireDate,
// Domain info available to the template
"Owner": service.owner,
"Host": service.host(),
"Label": service.label,
},
)
return derp.Wrap(err, "service.DomainEmail.SendWelcome", "Error sending welcome email to user", user.EmailAddress)
}
// SendPasswordReset sends a passowrd reset email to the user. This method
// swallows errors so that it can be run asynchronously.
func (service *DomainEmail) SendPasswordReset(user *model.User) error {
// Send the welcome email
err := service.serverEmail.Send(
service.smtp,
"user-password-reset",
service.owner.EmailAddress,
[]string{user.EmailAddress},
"Emissary Password Reset",
mapof.Any{
// User info available to the template
"UserID": user.UserID.Hex(),
"Username": user.Username,
"DisplayName": user.DisplayName,
"ResetCode": user.PasswordReset.AuthCode,
"ExpireDate": user.PasswordReset.ExpireDate,
// Domain info available to the template
"Owner": service.owner,
"Host": service.host(),
"Label": service.label,
},
)
return derp.Wrap(err, "service.DomainEmail.SendWelcome", "Error sending password reset email to user", user.Username)
}
func (service *DomainEmail) host() string {
return domain.Protocol(service.hostname) + service.hostname
}