-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
80 lines (66 loc) · 1.51 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
package notifications
import (
"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/marcel"
"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": "",
})
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
})
if err != nil {
log.Fatal(err)
}
svc = ses.New(sess)
}
type Email = marcel.Email
type Attachment = marcel.Attachment
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)
mime, err := email.ToMIME()
if err != nil {
return err
}
return SendRawEmail(mime)
}
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
}