-
Notifications
You must be signed in to change notification settings - Fork 10
/
email.go
168 lines (147 loc) · 3.79 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
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
package main
import (
"bytes"
"encoding/json"
"html/template"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/breez/server/breez"
)
const (
charset = "UTF-8"
)
func addresses(a string) (addr []*string) {
json.Unmarshal([]byte(a), &addr)
return
}
func sendEmail(to, cc, from, content, subject string) error {
sess, err := session.NewSession(&aws.Config{})
if err != nil {
log.Printf("Error in session.NewSession: %v", err)
return err
}
svc := ses.New(sess)
input := &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: addresses(cc),
ToAddresses: addresses(to),
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(charset),
Data: aws.String(content),
},
},
Subject: &ses.Content{
Charset: aws.String(charset),
Data: aws.String(subject),
},
},
Source: aws.String(from),
}
// Attempt to send the email.
result, err := svc.SendEmail(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
log.Println(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
log.Println(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
log.Println(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
default:
log.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Println(err.Error())
}
return err
}
log.Printf("Email sent with result:\n%v", result)
return nil
}
func sendCardOrderNotification(in *breez.OrderRequest) error {
var html bytes.Buffer
tpl := `
<div>FullName: {{ .FullName }}</div>
<div>Address: {{ .Address }}</div>
<div>City: {{ .City }}</div>
<div>State: {{ .State }}</div>
<div>Zip: {{ .Zip }}</div>
<div>Country: {{ .Country }}</div>
<div>Email: {{ .Email }}</div>
`
t, err := template.New("OrderCardEmail").Parse(tpl)
if err != nil {
return err
}
if err := t.Execute(&html, in); err != nil {
return err
}
err = sendEmail(
os.Getenv("CARD_NOTIFICATION_TO"),
os.Getenv("CARD_NOTIFICATION_CC"),
os.Getenv("CARD_NOTIFICATION_FROM"),
html.String(),
"Card Order",
)
if err != nil {
log.Printf("Error sending order card email: %v", err)
return err
}
return nil
}
func sendPaymentFailureNotification(in *breez.ReportPaymentFailureRequest, keys []string) error {
var html bytes.Buffer
tpl := `
<div>SdkVersion: {{ .SdkVersion }}</div>
<div>SdkGitHash: {{ .SdkGitHash }}</div>
<div>NodeId: {{ .NodeId }}</div>
<div>LspId: {{ .LspId }}</div>
<div>ApiKey: {{ .ApiKey }}</div>
<div>Timestamp: {{ .Timestamp }}</div>
<div>Comment/error: {{ .Comment }}</div>
<div>Report:</div>
<div>{{ .Report }}</div>
`
t, err := template.New("PaymentFailureEmail").Parse(tpl)
if err != nil {
log.Printf("Error parsing HTML template: %v", err)
return err
}
data := map[string]string{
"ApiKey": strings.Join(keys, ", "),
"Comment": in.Comment,
"LspId": in.LspId,
"NodeId": in.NodeId,
"Report": in.Report,
"SdkGitHash": in.SdkGitHash,
"SdkVersion": in.SdkVersion,
"Timestamp": in.Timestamp,
}
if err := t.Execute(&html, data); err != nil {
log.Printf("Error applying data to template: %v", err)
return err
}
err = sendEmail(
os.Getenv("PAYMENT_FAILURE_NOTIFICATION_TO"),
os.Getenv("PAYMENT_FAILURE_NOTIFICATION_CC"),
os.Getenv("PAYMENT_FAILURE_NOTIFICATION_FROM"),
html.String(),
"Payment Failure",
)
if err != nil {
log.Printf("Error sending payment failure email: %v", err)
return err
}
return nil
}