-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
sendmail.go
46 lines (38 loc) · 1.17 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
package staticbackend
import (
"log"
"net/http"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/email"
"github.com/staticbackendhq/core/middleware"
)
func sudoSendMail(w http.ResponseWriter, r *http.Request) {
var data email.SendMailData
if err := parseBody(r.Body, &data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// if only body is provided
if len(data.Body) > 0 {
data.HTMLBody = data.Body
data.TextBody = email.StripHTML(data.Body)
} else if len(data.TextBody) == 0 && len(data.HTMLBody) > 0 {
data.TextBody = email.StripHTML(data.HTMLBody)
} else if len(data.HTMLBody) == 0 && len(data.TextBody) > 0 {
data.HTMLBody = data.TextBody
}
if err := backend.Emailer.Send(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
config, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := backend.DB.IncrementMonthlyEmailSent(config.ID); err != nil {
//TODO: do something better with this error
log.Println("error increasing monthly email sent: ", err)
}
respond(w, http.StatusOK, true)
}