forked from hjr265/postmark.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmark.go
228 lines (202 loc) · 4.64 KB
/
postmark.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
217
218
219
220
221
222
223
224
225
226
227
228
// Package postmark provides a convenient wrapper for the Postmark API
package postmark
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/mail"
"net/url"
"strings"
)
const (
DefaultHost = "api.postmarkapp.com"
)
type Message struct {
From *mail.Address
To []*mail.Address
Cc []*mail.Address
Bcc []*mail.Address
Subject string
Tag string
HtmlBody io.Reader
TextBody io.Reader
TemplateId int
TemplateModel map[string]interface{}
ReplyTo *mail.Address
Headers mail.Header
Attachments []Attachment
TrackOpens bool
TrackLinks bool
}
func (m *Message) MarshalJSON() ([]byte, error) {
doc := &struct {
From string
To string
Cc string
Bcc string
Subject string `json:",omitempty"`
Tag string
HtmlBody string `json:",omitempty"`
TextBody string `json:",omitempty"`
TemplateId int `json:",omitempty"`
TemplateModel map[string]interface{}
ReplyTo string
Headers []map[string]string
Attachments []Attachment `json:",omitempty"`
TrackOpens bool
TrackLinks string
}{}
doc.From = m.From.String()
to := []string{}
for _, addr := range m.To {
to = append(to, addr.String())
}
doc.To = strings.Join(to, ", ")
cc := []string{}
for _, addr := range m.Cc {
cc = append(cc, addr.String())
}
doc.Cc = strings.Join(cc, ", ")
bcc := []string{}
for _, addr := range m.Bcc {
bcc = append(bcc, addr.String())
}
doc.Bcc = strings.Join(bcc, ", ")
doc.Subject = m.Subject
doc.Tag = m.Tag
if m.HtmlBody != nil {
htmlBody, err := ioutil.ReadAll(m.HtmlBody)
if err != nil {
return nil, err
}
doc.HtmlBody = string(htmlBody)
}
if m.TextBody != nil {
textBody, err := ioutil.ReadAll(m.TextBody)
if err != nil {
return nil, err
}
doc.TextBody = string(textBody)
}
doc.TemplateId = m.TemplateId
doc.TemplateModel = m.TemplateModel
if m.ReplyTo != nil {
doc.ReplyTo = m.ReplyTo.String()
}
headers := []map[string]string{}
for k, vs := range m.Headers {
for _, v := range vs {
headers = append(headers, map[string]string{
"Name": k,
"Value": v,
})
}
}
doc.Headers = headers
doc.Attachments = m.Attachments
if m.TrackLinks {
doc.TrackLinks = "HtmlOnly"
} else {
doc.TrackLinks = "None"
}
doc.TrackOpens = m.TrackOpens
return json.Marshal(doc)
}
type Attachment struct {
Name string
Content io.Reader
ContentType string
}
func (a *Attachment) MarshalJSON() ([]byte, error) {
doc := &struct {
Name string
Content string
ContentType string
}{}
doc.Name = a.Name
content, err := ioutil.ReadAll(a.Content)
if err != nil {
return nil, err
}
doc.Content = base64.StdEncoding.EncodeToString(content)
doc.ContentType = a.ContentType
return json.Marshal(doc)
}
type Client struct {
ApiKey string
Secure bool
Host string // Host for the API endpoints, DefaultHost if ""
}
func (c *Client) endpoint(path string) *url.URL {
url := &url.URL{}
if c.Secure {
url.Scheme = "https"
} else {
url.Scheme = "http"
}
if c.Host == "" {
url.Host = DefaultHost
} else {
url.Host = c.Host
}
url.Path = path
return url
}
// Send sends a single message
func (c *Client) Send(msg *Message) (*Result, error) {
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(msg)
if err != nil {
return nil, err
}
url := c.endpoint("email")
if msg.TemplateId != 0 {
url = c.endpoint("email/withTemplate")
}
req, err := http.NewRequest("POST", url.String(), &buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Postmark-Server-Token", c.ApiKey)
resp, err := (&http.Client{}).Do(req)
if err != nil {
return nil, err
}
res := &Result{}
json.NewDecoder(resp.Body).Decode(res)
return res, nil
}
// SendBatch sends multiple messages using the batch API
func (c *Client) SendBatch(msg []*Message) ([]*Result, error) {
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(msg)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.endpoint("email/batch").String(), &buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Postmark-Server-Token", c.ApiKey)
resp, err := (&http.Client{}).Do(req)
if err != nil {
return nil, err
}
res := []*Result{}
json.NewDecoder(resp.Body).Decode(res)
return res, nil
}
type Result struct {
ErrorCode int
Message string
MessageID string
SubmittedAt string
To string
}