forked from sendgrid/smtpapi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtpapi.go
216 lines (185 loc) · 6.47 KB
/
smtpapi.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package smtpapi
import (
"bytes"
"encoding/json"
"fmt"
"unicode/utf16"
)
// Version represents the current version of the smtpapi-go library
const Version = "0.4.2"
// SMTPAPIHeader will be used to set up X-SMTPAPI params
type SMTPAPIHeader struct {
To []string `json:"to,omitempty"`
Sub map[string][]string `json:"sub,omitempty"`
Section map[string]string `json:"section,omitempty"`
Category []string `json:"category,omitempty"`
UniqueArgs map[string]string `json:"unique_args,omitempty"`
Filters map[string]Filter `json:"filters,omitempty"`
ASMGroupID int `json:"asm_group_id,omitempty"`
ASMGroups []int `json:"asm_groups_to_display,omitempty"`
SendAt int64 `json:"send_at,omitempty"`
SendEachAt []int64 `json:"send_each_at,omitempty"`
IpPool string `json:"ip_pool,omitempty"`
BatchID string `json:"batch_id,omitempty"`
DynamicData map[string]interface{} `json:"dynamic_template_data,omitempty"`
}
// Filter represents an App/Filter and its settings
type Filter struct {
Settings map[string]interface{} `json:"settings,omitempty"`
}
// NewSMTPAPIHeader creates a new header struct
func NewSMTPAPIHeader() *SMTPAPIHeader {
return &SMTPAPIHeader{}
}
// AddTo appends a single email to the To header
func (h *SMTPAPIHeader) AddTo(email string) {
h.To = append(h.To, email)
}
// AddTos appends multiple emails to the To header
func (h *SMTPAPIHeader) AddTos(emails []string) {
for i := 0; i < len(emails); i++ {
h.AddTo(emails[i])
}
}
// SetTos sets the value of the To header
func (h *SMTPAPIHeader) SetTos(emails []string) {
h.To = emails
}
// AddSubstitution adds a new substitution to a specific key
func (h *SMTPAPIHeader) AddSubstitution(key, sub string) {
if h.Sub == nil {
h.Sub = make(map[string][]string)
}
h.Sub[key] = append(h.Sub[key], sub)
}
// AddSubstitutions adds a multiple substitutions to a specific key
func (h *SMTPAPIHeader) AddSubstitutions(key string, subs []string) {
for i := 0; i < len(subs); i++ {
h.AddSubstitution(key, subs[i])
}
}
// SetSubstitutions sets the value of the substitutions on the Sub header
func (h *SMTPAPIHeader) SetSubstitutions(sub map[string][]string) {
h.Sub = sub
}
// AddSection sets the value for a specific section
func (h *SMTPAPIHeader) AddSection(section, value string) {
if h.Section == nil {
h.Section = make(map[string]string)
}
h.Section[section] = value
}
// SetSections sets the value for the Section header
func (h *SMTPAPIHeader) SetSections(sections map[string]string) {
h.Section = sections
}
// AddCategory adds a new category to the Category header
func (h *SMTPAPIHeader) AddCategory(category string) {
h.Category = append(h.Category, category)
}
// AddCategories adds multiple categories to the Category header
func (h *SMTPAPIHeader) AddCategories(categories []string) {
for i := 0; i < len(categories); i++ {
h.AddCategory(categories[i])
}
}
// SetCategories will set the value of the Categories field
func (h *SMTPAPIHeader) SetCategories(categories []string) {
h.Category = categories
}
// SetASMGroupID will set the value of the ASMGroupID field
func (h *SMTPAPIHeader) SetASMGroupID(groupID int) {
h.ASMGroupID = groupID
}
// AddASMGroupToDisplay adds a new ASM group ID to be displayed
func (h *SMTPAPIHeader) AddASMGroupToDisplay(groupID int) {
h.ASMGroups = append(h.ASMGroups, groupID)
}
// AddASMGroupsToDisplay adds multiple ASM group IDs to be displayed
func (h *SMTPAPIHeader) AddASMGroupsToDisplay(groupIDs []int) {
for i := 0; i < len(groupIDs); i++ {
h.AddASMGroupToDisplay(groupIDs[i])
}
}
// SetASMGroupsToDisplay will set the value of the ASMGroups field
func (h *SMTPAPIHeader) SetASMGroupsToDisplay(groups []int) {
h.ASMGroups = groups
}
// AddUniqueArg will set the value of a specific argument
func (h *SMTPAPIHeader) AddUniqueArg(arg, value string) {
if h.UniqueArgs == nil {
h.UniqueArgs = make(map[string]string)
}
h.UniqueArgs[arg] = value
}
// SetUniqueArgs will set the value of the Unique_args header
func (h *SMTPAPIHeader) SetUniqueArgs(args map[string]string) {
h.UniqueArgs = args
}
// AddFilter will set the specific setting for a filter
func (h *SMTPAPIHeader) AddFilter(filter, setting string, value interface{}) {
if h.Filters == nil {
h.Filters = make(map[string]Filter)
}
if _, ok := h.Filters[filter]; !ok {
h.Filters[filter] = Filter{
Settings: make(map[string]interface{}),
}
}
h.Filters[filter].Settings[setting] = value
}
// SetFilter takes in a Filter struct with predetermined settings and sets it for such Filter key
func (h *SMTPAPIHeader) SetFilter(filter string, value *Filter) {
if h.Filters == nil {
h.Filters = make(map[string]Filter)
}
h.Filters[filter] = *value
}
// SetSendAt takes in a timestamp which determines when the email will be sent
func (h *SMTPAPIHeader) SetSendAt(sendAt int64) {
h.SendAt = sendAt
}
// AddSendEachAt takes in a timestamp and pushes it into a list Must match length of To emails
func (h *SMTPAPIHeader) AddSendEachAt(sendEachAt int64) {
h.SendEachAt = append(h.SendEachAt, sendEachAt)
}
// SetSendEachAt takes an array of timestamps. Must match length of To emails
func (h *SMTPAPIHeader) SetSendEachAt(sendEachAt []int64) {
h.SendEachAt = sendEachAt
}
// SetIpPool takes a strings and sets the IpPool field
func (h *SMTPAPIHeader) SetIpPool(ipPool string) {
h.IpPool = ipPool
}
// Unicode escape
func escapeUnicode(input string) string {
//var buffer bytes.Buffer
buffer := bytes.NewBufferString("")
for _, r := range input {
if r > 65535 {
// surrogate pair
var r1, r2 = utf16.EncodeRune(r)
var s = fmt.Sprintf("\\u%x\\u%x", r1, r2)
// error always nil https://golang.org/pkg/bytes/#Buffer.WriteString
buffer.WriteString(s) // nolint: gas, gosec
} else if r > 127 {
var s = fmt.Sprintf("\\u%04x", r)
// error always nil https://golang.org/pkg/bytes/#Buffer.WriteString
buffer.WriteString(s) // nolint: gas, gosec
} else {
var s = fmt.Sprintf("%c", r)
// error always nil https://golang.org/pkg/bytes/#Buffer.WriteString
buffer.WriteString(s) // nolint: gas, gosec
}
}
return buffer.String()
}
// JSONString returns the representation of the Header
func (h *SMTPAPIHeader) JSONString() (string, error) {
headers, e := json.Marshal(h)
return escapeUnicode(string(headers)), e
}
// Load allows you to load a pre-formed x-smtpapi header
func (h *SMTPAPIHeader) Load(b []byte) error {
return json.Unmarshal(b, h)
}