forked from bittersweet/notifilter-receive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
65 lines (56 loc) · 1.3 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
package main
import (
"bytes"
"log"
"net/smtp"
"text/template"
)
type emailNotifier struct {
}
const emailTemplate = `From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
MIME-version: 1.0
Content-Type: text/html; charset="UTF-8"
<html>
<body>
{{.Body}}
</body>
</html>`
type EmailData struct {
From string
To string
Subject string
Body string
}
func (e *emailNotifier) sendMessage(event_name string, data []byte) NotifierResponse {
var err error
var doc bytes.Buffer
t := template.New("emailTemplate")
t, err = t.Parse(emailTemplate)
if err != nil {
log.Fatal("t.Parse ", err)
}
context := &EmailData{
"Springest Dev <[email protected]>",
"Email subject line",
string(data),
}
err = t.Execute(&doc, context)
if err != nil {
log.Fatal("t.Execute ", err)
}
// TODO: setup env variables to support multiple envs
// TODO: Set up real test mode instead of using mailcatcher
auth := smtp.PlainAuth("", "", "", "localhost:1025")
err = smtp.SendMail("localhost:1025", auth, "[email protected]", []string{"[email protected]"}, doc.Bytes())
if err != nil {
log.Fatal("smtp.SendMail ", err)
}
// drop e-mail job on a rate limited (max workers) queue
// already experienced a connection reset by peer locally
return NotifierResponse{
error: err,
}
}