-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
189 lines (156 loc) · 3.59 KB
/
notifications.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
package notifications
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"text/template"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/davidbanham/required_env"
)
var svc *ses.SES
var testMode bool
var debugLogging bool
var tmpl *template.Template
var attachmentTmpl *template.Template
func init() {
if os.Getenv("TEST_MOCKS_ON") == "true" {
testMode = true
return
}
if os.Getenv("NOTIFICATIONS_LOG_LEVEL") == "debug" {
debugLogging = true
}
required_env.Ensure(map[string]string{
"AWS_ACCESS_KEY_ID": "",
"AWS_SECRET_ACCESS_KEY": "",
})
var err error
tmpl, err = template.New("email").Parse(`From: {{.From}}
To: {{.To}}
{{ if .ReplyTo }}Reply-To: {{.ReplyTo}}{{ end }}
Subject: {{.Subject}}
MIME-Version: 1.0
Content-type: multipart/mixed;
boundary="NextPart"
--NextPart
Content-type: multipart/alternative;
boundary="AlternativePart"
{{ if .Text }}
--AlternativePart
Content-Type: text/plain
{{.Text}}
{{ end}}
{{ if .HTML }}
--AlternativePart
Content-Type: text/html
{{.HTML}}
{{ end }}
--AlternativePart--
`)
if err != nil {
log.Fatal(err)
}
attachmentTmpl, err = template.New("attachment").Parse(`--NextPart
Content-Type: {{.ContentType}};
Content-Disposition: attachment;
filename="{{.Filename}}"
`)
if err != nil {
log.Fatal(err)
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
})
if err != nil {
log.Fatal(err)
}
svc = ses.New(sess)
}
type Email struct {
To string
From string
ReplyTo string
Text string
HTML string
Subject string
Attachments []Attachment
}
type Attachment struct {
ContentType string
Data io.Reader
Filename string
}
func (attachment Attachment) execute(data io.Writer) error {
if err := attachmentTmpl.Execute(data, attachment); err != nil {
return err
}
_, err := io.Copy(data, attachment.Data)
return err
}
func (attachment *Attachment) MarshalJSON() ([]byte, error) {
data, err := ioutil.ReadAll(attachment.Data)
if err != nil {
return []byte{}, err
}
return json.Marshal(&struct {
ContentType string `json:"content_type"`
Data []byte `json:"data"`
Filename string `json:"filename"`
}{
ContentType: attachment.ContentType,
Data: data,
Filename: attachment.Filename,
})
}
func (attachment *Attachment) UnmarshalJSON(data []byte) error {
inner := struct {
ContentType string `json:"content_type"`
Data []byte `json:"data"`
Filename string `json:"filename"`
}{}
if err := json.Unmarshal(data, &inner); err != nil {
return err
}
attachment.ContentType = inner.ContentType
attachment.Filename = inner.Filename
attachment.Data = bytes.NewReader(inner.Data)
return nil
}
func SendEmail(email Email) error {
if debugLogging {
log.Printf("DEBUG notifications email: %+v \n", email)
}
if testMode {
log.Println("INFO notifications TESTMODE dropping email to", email.To, "from", email.From)
return nil
}
log.Println("INFO notifications sending email to", email.To, "from", email.From)
data := bytes.Buffer{}
if err := tmpl.Execute(&data, email); err != nil {
return err
}
for _, attachment := range email.Attachments {
if err := attachment.execute(&data); err != nil {
return nil
}
}
return SendRawEmail(data.Bytes())
}
func SendRawEmail(data []byte) error {
if testMode {
log.Println("INFO notifications TESTMODE dropping raw email")
return nil
}
input := &ses.SendRawEmailInput{
RawMessage: &ses.RawMessage{
Data: data,
},
}
_, err := svc.SendRawEmail(input)
return err
}