-
Notifications
You must be signed in to change notification settings - Fork 14
/
mandrill_sender.go
168 lines (137 loc) · 3.74 KB
/
mandrill_sender.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 mail
import (
"errors"
"fmt"
"github.com/mattbaird/gochimp"
"github.com/coretrix/hitrix/service/component/config"
)
const (
// ref: https://github.com/shawnmclean/Mandrill-dotnet/blob/05f26c917264751a903e3bcf83ca7153b5656526/src/Mandrill/Models/EmailMessage.cs#L19
mergeLanguageHandlebars = "handlebars"
)
type Mandrill struct {
client *gochimp.MandrillAPI
defaultFromEmail string
fromName string
}
func NewMandrill(configService config.IConfig) (IProvider, error) {
apiKey, ok := configService.String("mail.mandrill.api_key")
if !ok {
return nil, errors.New("mail.mandrill.api_key is missing")
}
fromEmail, ok := configService.String("mail.mandrill.default_from_email")
if !ok {
return nil, errors.New("mail.mandrill.default_from_email is missing")
}
fromName, ok := configService.String("mail.mandrill.from_name")
if !ok {
return nil, errors.New("mail.mandrill.from_name is missing")
}
mandrillAPI, err := gochimp.NewMandrill(apiKey)
if err != nil {
panic(err)
}
return &Mandrill{client: mandrillAPI, defaultFromEmail: fromEmail, fromName: fromName}, nil
}
func (s *Mandrill) GetTemplateKeyFromConfig(configService config.IConfig, templateName string) (string, error) {
configPath := fmt.Sprintf("mail.mandrill.templates.%s", templateName)
templateKey, ok := configService.String(configPath)
if !ok {
return "", fmt.Errorf("could not find email template key in config: %s", configPath)
}
return templateKey, nil
}
func (s *Mandrill) SendTemplate(message *Message) error {
return s.sendTemplate(
message.From,
message.FromName,
message.To,
message.ReplyTo,
message.Subject,
message.TemplateName,
message.TemplateData,
nil)
}
func (s *Mandrill) SendTemplateWithAttachments(message *MessageAttachment) error {
var attachments []gochimp.Attachment
if message.Attachments != nil {
attachments = make([]gochimp.Attachment, len(message.Attachments))
for i, attachment := range message.Attachments {
attachments[i] = gochimp.Attachment{
Type: attachment.ContentType,
Name: attachment.Filename,
Content: attachment.Base64Content,
}
}
}
return s.sendTemplate(
message.From,
message.FromName,
message.To,
message.ReplyTo,
message.Subject,
message.TemplateName,
message.TemplateData,
attachments)
}
func (s *Mandrill) sendTemplate(
from string,
fromName string,
to string,
replyTo string,
subject string,
templateName string,
templateData interface{},
attachments []gochimp.Attachment,
) error {
if from == "" {
from = s.defaultFromEmail
}
message := gochimp.Message{
MergeLanguage: mergeLanguageHandlebars,
Subject: subject,
FromEmail: from,
Attachments: attachments,
To: []gochimp.Recipient{
{Email: to},
},
}
if fromName != "" {
message.FromName = fromName
}
if replyTo != "" {
message.Headers = map[string]string{
"Reply-To": replyTo,
}
}
var templateContent []gochimp.Var
if templateData != nil {
for key, value := range templateData.(map[string]interface{}) {
templateContent = append(templateContent, *gochimp.NewVar(key, value))
}
}
message.AddMergeVar(gochimp.MergeVars{Recipient: to, Vars: templateContent})
responses, err := s.client.MessageSendTemplate(templateName, templateContent, message, false)
if err != nil {
return err
}
if responses != nil {
var errAsString string
for _, response := range responses {
if response.RejectedReason != "" {
errAsString += response.RejectedReason + " | "
}
}
if errAsString != "" {
return errors.New(errAsString)
}
}
return nil
}
func (s *Mandrill) GetTemplateHTMLCode(templateName string) (string, error) {
template, err := s.client.TemplateInfo(templateName)
if err != nil {
return "", err
}
return template.Code, nil
}