-
Notifications
You must be signed in to change notification settings - Fork 327
/
smtp_configuration_checker.go
207 lines (163 loc) · 4.96 KB
/
smtp_configuration_checker.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package service
import (
"context"
"crypto/tls"
"fmt"
intAuth "github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/mail"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/system/types"
gomail "gopkg.in/mail.v2"
htpl "html/template"
"io/ioutil"
"strings"
)
type (
smtpConfigurationChecker struct {
settings *types.AppSettings
ts TemplateService
opt options.AuthOpt
accessControl smtpCheckAccessController
}
smtpCheckAccessController interface {
CanManageSettings(context.Context) bool
}
)
func SmtpConfigurationChecker(s *types.AppSettings, ts TemplateService, ac accessController, opt options.AuthOpt) *smtpConfigurationChecker {
return &smtpConfigurationChecker{
settings: s,
ts: ts,
opt: opt,
accessControl: ac,
}
}
// Check SMTP server configurations and send a test email
// to recipients if they're provided
func (svc smtpConfigurationChecker) Check(ctx context.Context, smtpConfigs *types.SmtpConfiguration) (checkResults *types.SmtpCheckResult, err error) {
if !svc.accessControl.CanManageSettings(ctx) {
return nil, fmt.Errorf("not allowed to check SMTP configurations")
}
var (
tlsConfig = &tls.Config{}
)
checkResults = &types.SmtpCheckResult{}
if smtpConfigs.Port == 0 {
smtpConfigs.Port = 25
}
//check for validity of the host
if !mail.IsValidHost(smtpConfigs.Host) {
checkResults.Host = fmt.Sprintf("%s name is Invalid", smtpConfigs.Host)
}
// Applying TLS
tlsConfig = &tls.Config{ServerName: smtpConfigs.Host}
if smtpConfigs.TLSInsecure {
tlsConfig.InsecureSkipVerify = true
}
if smtpConfigs.TLSServerName != "" {
tlsConfig.ServerName = smtpConfigs.TLSServerName
}
checkResults.Server = mail.ConfigCheck(smtpConfigs.Host, smtpConfigs.Port, smtpConfigs.Username, smtpConfigs.Password, tlsConfig)
//send the email there are recipients
if checkResults.Server == "" {
if len(smtpConfigs.Recipients) != 0 {
checkResults.Send, err = svc.smtpSend(ctx, smtpConfigs.Recipients)
}
}
return checkResults, err
}
func (svc smtpConfigurationChecker) smtpSend(ctx context.Context, recipients []string) (expected string, err error) {
var (
ntf = mail.New()
toHeader string
// context with service user
// we need this for retrieving & rendering email templates
suCtx = intAuth.SetIdentityToContext(ctx, intAuth.ServiceUser())
)
if err = svc.procEmailRecipients(ntf, "To", recipients); err != nil {
return "", err
}
toHeader = strings.Join(recipients, ",")
ntf.SetAddressHeader("To", toHeader, "")
st, ct, err := svc.findEmailTemplates(suCtx)
// if we cannot find an email template
if err != nil {
ntf.SetHeader("Subject", "SMTP Configuration check")
ntf.SetBody("text/html", "<h2 style=\"color: #568ba2;text-align: center;\">SMTP configurations check passed</h2>")
err = mail.Send(ntf)
if err != nil {
return err.Error(), nil
}
return "", nil
}
subjectTmp, contentTmp, err := svc.procEmailTemplate(suCtx, st.ID, ct.ID)
if err != nil {
return "", err
}
ntf.SetHeader("Subject", string(subjectTmp))
ntf.SetBody("text/html", string(contentTmp))
err = mail.Send(ntf)
if err != nil {
return err.Error(), nil
}
return "", nil
}
func (svc smtpConfigurationChecker) procEmailRecipients(m *gomail.Message, field string, recipients []string) (err error) {
var (
email string
rcpt string
)
if len(recipients) == 0 {
return
}
for _, rcpt = range recipients {
email = strings.TrimSpace(rcpt)
// Validate email here
if !mail.IsValidAddress(email) {
return fmt.Errorf("invalid recipient email address %s", email)
}
}
m.SetHeader(field, recipients...)
return nil
}
// procEmailTemplate processes Email address template based on the template's subject ID and content ID
func (svc smtpConfigurationChecker) procEmailTemplate(ctx context.Context, stId uint64, ctId uint64) (subjectTmp []byte, contentTmp []byte, err error) {
// Prepare payload
payload := map[string]interface{}{
"Logo": htpl.URL(svc.settings.General.Mail.Logo),
"BaseURL": svc.opt.BaseURL,
}
// Render document
subject, err := svc.ts.Render(ctx, stId, "text/plain", payload, nil)
if err != nil {
return nil, nil, err
}
content, err := svc.ts.Render(ctx, ctId, "text/plain", payload, nil)
if err != nil {
return nil, nil, err
}
subjectTmp, err = ioutil.ReadAll(subject)
if err != nil {
return nil, nil, err
}
contentTmp, err = ioutil.ReadAll(content)
if err != nil {
return nil, nil, err
}
return subjectTmp, contentTmp, nil
}
func (svc smtpConfigurationChecker) findEmailTemplates(ctx context.Context) (st *types.Template, ct *types.Template, err error) {
var (
hdl string
)
hdl = "smtp_configuration_check_subject"
st, err = svc.ts.FindByHandle(ctx, hdl)
if err != nil {
return nil, nil, err
}
hdl = "smtp_configuration_check_content"
ct, err = svc.ts.FindByHandle(ctx, hdl)
if err != nil {
return nil, nil, err
}
return st, ct, nil
}