-
Notifications
You must be signed in to change notification settings - Fork 1
/
email.go
206 lines (169 loc) · 4.65 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
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
package csv2table
import (
"bytes"
"html/template"
"net/smtp"
"github.com/jordan-wright/email"
)
// Email holds configuration of the email notification
// Currently it supports SMTP with PlainAuth
type Email struct {
SendOnSuccess bool // send email on success
SendOnError bool // send email on error
SuccessSubject string
SuccessBody string
ErrorSubject string
ErrorBody string
From string
To []string
Cc []string
Bcc []string
SMTPServer string // including port e.g. smtp.gmail.com:587
// config for smtp.PlainAuth
PlainAuth struct {
Identity string
Username string
Password string
Host string
}
}
// EmailTemplateContext is the template context that will be passed to subject and body parser
type EmailTemplateContext struct {
SuccessCount int
ErrorCount int
Files []ImportFileStatus
}
const (
defaultSendOnSuccess = false
defaultSendOnError = false
)
// subject templates
// available variables: {{SuccessCount}, {{ErrorCount}}
const (
defaultSuccessSubject = "csv2table: imported {{.SuccessCount}} file(s)"
defaultErrorSubject = "csv2table: import errors"
)
// emial body templates
// available variables: {{SuccessCount}}, {{ErrorCount}, {{Files}}
const (
defaultSuccessBody = `
Hello,<br/><br/>
{{if .SuccessCount}}
Successfully imported {{.SuccessCount}} file(s).<br/>
{{end}}
{{if .ErrorCount}}
<span style="color:red">{{.ErrorCount}} file(s) produced errors.</span><br/>
{{end}}
<ol>
{{range .Files}}
<li>
{{if .Error}}
{{.FileName}}: Error: {{.Error}}
{{else}}
{{.FileName}}: Imported {{.RowCount}} rows
{{end}}
</li>
{{end}}
</ol>
Bye.
`
defaultErrorBody = defaultSuccessBody
)
// emailConfig is the global email configuration
var emailConfig = Email{
SendOnSuccess: defaultSendOnSuccess,
SendOnError: defaultSendOnError,
SuccessSubject: defaultSuccessSubject,
SuccessBody: defaultSuccessBody,
ErrorSubject: defaultErrorSubject,
ErrorBody: defaultErrorBody,
}
// emailConfigured checks if email configuration is present
func emailConfigured() bool {
return emailConfig.SMTPServer != "" && emailConfig.From != "" && len(emailConfig.To) > 0
}
// createTemplateContext creates the template context used in subject and body parsing
func createTemplateContext(statuses []ImportFileStatus) EmailTemplateContext {
var successCount, errorCount int
var status ImportFileStatus
for _, status = range statuses {
if status.Error != nil {
errorCount++
} else {
successCount++
}
}
return EmailTemplateContext{
SuccessCount: successCount,
ErrorCount: errorCount,
Files: statuses,
}
}
// parseTemplate parses an email template (subject or body)
func parseTemplate(tpl string, context EmailTemplateContext) (string, error) {
// create subject
t, err := template.New("email").Parse(tpl)
if err != nil {
return "", err
}
var output bytes.Buffer
err = t.Execute(&output, context)
if err != nil {
return "", err
}
return output.String(), nil
}
// sendEmailSuccess delivers a success email after an import successfully finished
func sendEmailSuccess(statuses []ImportFileStatus) error {
e := &email.Email{
To: emailConfig.To,
From: emailConfig.From,
Subject: emailConfig.SuccessSubject,
Text: []byte(emailConfig.SuccessBody),
}
var err error
// create subject
context := createTemplateContext(statuses)
e.Subject, err = parseTemplate(emailConfig.SuccessSubject, context)
if err != nil {
return err
}
text, err := parseTemplate(emailConfig.SuccessBody, context)
if err != nil {
return err
}
e.HTML = []byte(text)
// fmt.Println(e.Subject)
// fmt.Println(string(e.HTML))
// return nil
err = e.Send(emailConfig.SMTPServer, smtp.PlainAuth(emailConfig.PlainAuth.Identity, emailConfig.PlainAuth.Username,
emailConfig.PlainAuth.Password, emailConfig.PlainAuth.Host))
return err
}
// sendEmailError delivers a success email after an import successfully finished
func sendEmailError(statuses []ImportFileStatus) error {
e := &email.Email{
To: emailConfig.To,
From: emailConfig.From,
Subject: emailConfig.SuccessSubject,
Text: []byte(emailConfig.SuccessBody),
}
var err error
// create subject
context := createTemplateContext(statuses)
e.Subject, err = parseTemplate(emailConfig.ErrorSubject, context)
if err != nil {
return err
}
text, err := parseTemplate(emailConfig.ErrorBody, context)
if err != nil {
return err
}
e.HTML = []byte(text)
// fmt.Println(e.Subject)
// fmt.Println(string(e.HTML))
// return nil
err = e.Send(emailConfig.SMTPServer, smtp.PlainAuth(emailConfig.PlainAuth.Identity, emailConfig.PlainAuth.Username,
emailConfig.PlainAuth.Password, emailConfig.PlainAuth.Host))
return err
}