-
Notifications
You must be signed in to change notification settings - Fork 16
/
sendmail.go
328 lines (279 loc) · 9.75 KB
/
sendmail.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright 2020 Rodrigue Chakode and contributors.
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 main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"html/template"
"net"
"net/http"
"net/smtp"
"net/url"
"strings"
"log"
"github.com/dpapathanasiou/go-recaptcha"
"github.com/spf13/viper"
)
type SendMailRequest struct {
from string
to []string
subject string
body string
}
type ContactRequest struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Organization string `json:"organization,omitempty"`
Subject string `json:"subject,omitempty"`
Message string `json:"message,omitempty"`
RequestTarget string `json:"requestType,omitempty"`
OriginURI string `json:"originURI,omitempty"`
}
type ContactResponse struct {
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
}
// NewSendMailRequest creates a new instance to manage send mail
func NewSendMailRequest(from string, to []string, subject string) *SendMailRequest {
return &SendMailRequest{
from: from,
to: to,
subject: subject,
}
}
// Execute processes the actual email sending
func (m *SendMailRequest) Execute() error {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
from := "From: " + m.from + "\n"
subject := "Subject: " + m.subject + "\n"
msg := []byte(from + subject + mime + "\n" + m.body)
// Connect to the SMTP Server
smtpServerAddr := viper.GetString("SMTP_SERVER_ADDR")
smtpServerHost, _, _ := net.SplitHostPort(smtpServerAddr)
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: viper.GetBool("SMTP_SKIP_VERIFY_CERT"),
ServerName: smtpServerHost,
}
// Important: call tls.Dial instead of smtp.Dial for smtp servers running on 465.
// On port 465 ssl connection is required from the very beginning (no starttls)
conn, err := tls.Dial("tcp", smtpServerAddr, tlsconfig)
if err != nil {
return fmt.Errorf("failed initiating smtp connection to host %s (%s)", smtpServerAddr, err)
}
defer conn.Close()
smtpClient, err := smtp.NewClient(conn, smtpServerHost)
if err != nil {
return fmt.Errorf("failed creating smtp client to host %s (%s)", smtpServerHost, err)
}
defer smtpClient.Quit()
// Authenticate if configured
if viper.GetBool("SMTP_AUTHENTICATION_ENABLED") {
smtpClientAuth := smtp.PlainAuth("",
viper.GetString("SMTP_CLIENT_USERNAME"),
viper.GetString("SMTP_CLIENT_PASSWORD"),
smtpServerHost)
if err = smtpClient.Auth(smtpClientAuth); err != nil {
return fmt.Errorf("failed authenticating to smtp server (%s)", err)
}
}
// Initialize a mail transaction
err = smtpClient.Mail(m.from)
if err != nil {
return fmt.Errorf("failed issuing MAIL command (%s)", err)
}
// Set recipients
for _, recipient := range m.to {
err = smtpClient.Rcpt(recipient)
if err != nil {
return fmt.Errorf("failed issuing RCPT command (%s)", err)
}
}
smtpWriter, err := smtpClient.Data()
if err != nil {
return fmt.Errorf("failed issuing DATA command (%s)", err)
}
defer smtpWriter.Close()
_, err = smtpWriter.Write([]byte(msg))
if err != nil {
return fmt.Errorf("failed sending mail content (%s)", err)
}
return nil
}
// ParseTemplate parses template and bing data and process the email sending
func (m *SendMailRequest) ParseTemplate(templateFileName string, data interface{}) error {
emailTpl, err := template.ParseFiles(templateFileName)
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = emailTpl.Execute(buf, data)
if err != nil {
return err
}
m.body = buf.String()
return nil
}
// MuxSecAllowedDomainsHandler is a security middleware which controls allowed domains.
func MuxSecAllowedDomainsHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
allowedDomains := strings.Split(viper.GetString("ALLOWED_ORIGINS"), ",")
allowedOrigins := make(map[string]bool)
for _, domain := range allowedDomains {
domainTrimmed := strings.TrimSpace(domain)
allowedOrigins[fmt.Sprintf("http://%s", domainTrimmed)] = true
allowedOrigins[fmt.Sprintf("https://%s", domainTrimmed)] = true
allowedOrigins[fmt.Sprintf("http://www.%s", domainTrimmed)] = true
allowedOrigins[fmt.Sprintf("https://www.%s", domainTrimmed)] = true
}
if len(r.Header["Origin"]) == 0 || len(r.Header["Referer"]) == 0 {
rawHeader, _ := json.Marshal(r.Header)
log.Println("request with unexpected headers", string(rawHeader))
w.WriteHeader(http.StatusForbidden)
return
}
reqOrigin := r.Header["Origin"][0]
if _, domainFound := allowedOrigins[reqOrigin]; !domainFound {
log.Println("not allowed origin", reqOrigin)
w.WriteHeader(http.StatusForbidden)
return
}
// Send the allowed origin back as CORS header
w.Header().Set("Access-Control-Allow-Origin", r.Header["Origin"][0])
next.ServeHTTP(w, r)
})
}
// MuxSecReCaptchaHandler is a security middleware which verifies the challenge code from
// the reCaptcha human verification system (provided by Google).
func MuxSecReCaptchaHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
recaptchaResponse, found := r.Form["g-recaptcha-response"]
if found {
remoteIp, _, _ := net.SplitHostPort(r.RemoteAddr)
recaptchaPrivateKey := viper.GetString("RECAPTCHA_PRIVATE_KEY")
recaptcha.Init(recaptchaPrivateKey)
result, err := recaptcha.Confirm(remoteIp, recaptchaResponse[0])
if err != nil {
log.Println("reCaptcha server error:", err.Error())
w.WriteHeader(http.StatusForbidden)
return
}
if !result {
w.WriteHeader(http.StatusForbidden)
return
}
}
next.ServeHTTP(w, r)
})
}
// SendMail handles HTTP request to send email
func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
httpReq.ParseForm()
contactRequest := ContactRequest{
Name: httpReq.FormValue("name"),
Email: strings.TrimSpace(httpReq.FormValue("email")),
Organization: httpReq.FormValue("organization"),
Subject: httpReq.FormValue("subject"),
Message: httpReq.FormValue("message"),
RequestTarget: httpReq.FormValue("target"),
OriginURI: httpReq.FormValue("requestOrigin"),
}
var recipients []string
switch contactRequest.RequestTarget {
case "demo":
recipients = []string{contactRequest.Email, viper.GetString("CONTACT_REPLY_BCC_EMAIL")}
case "contact":
recipients = []string{viper.GetString("CONTACT_REPLY_BCC_EMAIL")}
default:
log.Println("not allowed request type:", contactRequest.RequestTarget)
httpResp.WriteHeader(http.StatusForbidden)
httpResp.Write([]byte(`{"status": "error", "message": "unauthorized request"}`))
return
}
userData, _ := json.Marshal(contactRequest)
log.Println("New Request:", string(userData))
templateData := struct {
Name string
Email string
Organization string
Subject string
Message string
DemoURL string
}{
Name: contactRequest.Name,
Email: contactRequest.Email,
Organization: contactRequest.Organization,
Subject: contactRequest.Subject,
Message: contactRequest.Message,
DemoURL: viper.GetString("DEMO_URL"),
}
replyTplFile := ""
if contactRequest.RequestTarget == "demo" {
replyTplFile = viper.GetString("TEMPLATE_DEMO_REQUEST_REPLY")
if replyTplFile == "" {
replyTplFile = "./templates/template_reply_demo_request.html"
}
} else {
replyTplFile = viper.GetString("TEMPLATE_CONTACT_REQUEST_REPLY")
if replyTplFile == "" {
replyTplFile = "./templates/template_reply_contact_request.html"
}
}
contactEmail := viper.GetString("CONTACT_REPLY_EMAIL")
sendMailReq := NewSendMailRequest(
contactEmail,
recipients,
contactRequest.Subject,
)
err := sendMailReq.ParseTemplate(replyTplFile, templateData)
contactResponse := ContactResponse{}
if err == nil {
err := sendMailReq.Execute()
if err != nil {
log.Println(err.Error())
contactResponse.Status = "error"
contactResponse.Message = fmt.Sprintf("An internal error occurred, please try later or send us an email at %s.", viper.GetString("CONTACT_REPLY_BCC_EMAIL"))
} else {
contactResponse.Status = "success"
if contactRequest.RequestTarget == "demo" {
contactResponse.Message = "Thank you, if you supplied a correct email address then an email should have been sent to you."
} else {
contactResponse.Message = "Thank you, if you supplied a correct email address then we'll process your request within the next 48 hours."
}
}
} else {
log.Println(err.Error())
contactResponse.Status = "error"
contactResponse.Message = "Invalid request, please review your input and try again."
}
if contactRequest.OriginURI != "" {
originURL, err := url.Parse(contactRequest.OriginURI)
if err != nil {
log.Printf("error parsing the origin URL %s (%s)", originURL, err.Error())
originURL = &url.URL{} // continue with default (empty) url
}
q := originURL.Query()
q.Set("status", contactResponse.Status)
q.Set("message", contactResponse.Message)
originURL.RawQuery = q.Encode()
httpResp.Header().Set("Location", originURL.String())
httpResp.WriteHeader(http.StatusSeeOther)
} else {
httpResp.WriteHeader(http.StatusOK)
}
respRawData, _ := json.Marshal(contactResponse)
httpResp.Header().Set("Content-Type", "application/json; charset=UTF-8")
httpResp.Write(respRawData)
}