forked from TykTechnologies/tyk-email-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazonses.go
91 lines (73 loc) · 2.35 KB
/
amazonses.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
package emaildriver
import (
"bytes"
"errors"
"github.com/sourcegraph/go-ses"
)
type AmazonSESEmailBackend struct {
isEnabled bool
endpoint string
accessKeyId string
secretAccessKey string
config ses.Config
}
func (s *AmazonSESEmailBackend) Init(conf map[string]string) error {
var ok bool
if conf == nil {
return errors.New("AmazonSESEmailBackend requires a configuration map")
}
s.endpoint, ok = conf["Endpoint"]
if !ok {
return errors.New("No Amazon SES endpoint defined, emails will fail")
}
s.accessKeyId, ok = conf["AccessKeyId"]
if !ok {
return errors.New("No Mailgun private client key defined, emails will fail")
}
s.secretAccessKey, ok = conf["SecretAccessKey"]
if !ok {
return errors.New("No Mailgun public client key defined, emails will fail")
}
s.config = ses.Config{
Endpoint: s.endpoint,
AccessKeyID: s.accessKeyId,
SecretAccessKey: s.secretAccessKey,
}
s.isEnabled = true
return nil
}
func (s *AmazonSESEmailBackend) Send(emailMeta EmailMeta, emailData interface{}, textTemplateName TykTemplateName, htmlTemplateName TykTemplateName, OrgId string, Styles string) error {
if !s.isEnabled {
log.Warning("Plese check your email settings and restart Tyk Dashboard in order for notifications to work")
return errors.New("Amazon SES email driver not initialised correctly")
}
// Generate strings from templates
var htmlDoc, txtDoc bytes.Buffer
type superEmailData struct {
Data interface{}
Styles string
}
thisData := superEmailData{Data: emailData}
// Pull custom CSS
thisData.Styles = Styles
htmlErr := PortalEmailTemplatesHTML.ExecuteTemplate(&htmlDoc, string(htmlTemplateName), thisData)
if htmlErr != nil {
log.Error("HTML Template error: ", htmlErr)
return htmlErr
}
txtErr := PortalEmailTemplatesTXT.ExecuteTemplate(&txtDoc, string(textTemplateName), emailData)
if txtErr != nil {
log.Error("HTML Template error: ", txtErr)
return txtErr
}
from := "\"" + emailMeta.FromName + "\" <" + emailMeta.FromEmail + ">"
to := "\"" + emailMeta.RecipientName + "\" <" + emailMeta.RecipientEmail + ">"
response, err := s.config.SendEmailHTML(from, to, emailMeta.Subject, txtDoc.String(), htmlDoc.String())
log.Info("Email sending (AmazonSES)")
if err != nil {
log.Error("Amazon SES error: ", err)
log.Error("Response: ", response)
return err
}
return nil
}