-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmandrill.go
82 lines (63 loc) · 1.98 KB
/
mandrill.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
package emaildriver
import (
"bytes"
"errors"
ma "github.com/keighl/mandrill"
)
type MandrillEmailBackend struct {
clientKey string
isEnabled bool
}
func (m *MandrillEmailBackend) Init(conf map[string]string) error {
var ok bool
if conf == nil {
return errors.New("MandrillEmailBackend requires a configuration map")
}
m.clientKey, ok = conf["ClientKey"]
if !ok {
return errors.New("No Mandrill client key defined, emails will fail")
}
m.isEnabled = true
return nil
}
func (m *MandrillEmailBackend) Send(emailMeta EmailMeta, emailData interface{}, textTemplateName TykTemplateName, htmlTemplateName TykTemplateName, OrgId string, Styles string) error {
if !m.isEnabled {
log.Warning("Plese check your email settings and restart Tyk Dashboard in order for notifications to work")
return errors.New("Driver not initialised correctly")
}
// Generate strings from templates
var htmlDoc, txtDoc bytes.Buffer
type superEmailData struct {
Data interface{}
Styles string
}
thisData := superEmailData{Data: emailData}
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
}
// Do sending
client := ma.ClientWithKey(m.clientKey)
message := &ma.Message{}
message.AddRecipient(emailMeta.RecipientEmail, emailMeta.RecipientName, "to")
message.FromEmail = emailMeta.FromEmail
message.FromName = emailMeta.FromName
message.Subject = emailMeta.Subject
message.HTML = htmlDoc.String()
message.Text = txtDoc.String()
responses, err := client.MessagesSend(message)
//log.Warning("Email sending (Mandrill): ", responses)
if err != nil {
log.Error("Mandrill error: ", err)
log.Error("Responses: ", responses)
return err
}
return nil
}