This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
forked from ti55987/postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
234 lines (206 loc) · 7.91 KB
/
templates.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
229
230
231
232
233
234
package postmark
import (
"fmt"
"net/url"
)
// Template represents an email template on the server
type Template struct {
// TemplateId: ID of template
TemplateId int64
// Name: Name of template
Name string
// Subject: The content to use for the Subject when this template is used to send email.
Subject string
// HtmlBody: The content to use for the HtmlBody when this template is used to send email.
HtmlBody string
// TextBody: The content to use for the TextBody when this template is used to send email.
TextBody string
// AssociatedServerId: The ID of the Server with which this template is associated.
AssociatedServerId int64
// Active: Indicates that this template may be used for sending email.
Active bool
}
// TemplateInfo is a limited set of template info returned via Index/Editing endpoints
type TemplateInfo struct {
// TemplateId: ID of template
TemplateId int64
// Name: Name of template
Name string
// Active: Indicates that this template may be used for sending email.
Active bool
}
///////////////////////////////////////
///////////////////////////////////////
// GetTemplate fetches a specific template via TemplateID
func (client *Client) GetTemplate(templateID string) (Template, error) {
res := Template{}
err := client.doRequest(parameters{
Method: "GET",
Path: fmt.Sprintf("templates/%s", templateID),
TokenType: server_token,
}, &res)
return res, err
}
///////////////////////////////////////
///////////////////////////////////////
type templatesResponse struct {
TotalCount int64
Templates []TemplateInfo
}
// GetTemplates fetches a list of templates on the server
// It returns a TemplateInfo slice, the total template count, and any error that occurred
// Note: TemplateInfo only returns a subset of template attributes, use GetTemplate(id) to
// retrieve all template info.
func (client *Client) GetTemplates(count int64, offset int64) ([]TemplateInfo, int64, error) {
res := templatesResponse{}
values := &url.Values{}
values.Add("count", fmt.Sprintf("%d", count))
values.Add("offset", fmt.Sprintf("%d", offset))
err := client.doRequest(parameters{
Method: "GET",
Path: fmt.Sprintf("templates?%s", values.Encode()),
TokenType: server_token,
}, &res)
return res.Templates, res.TotalCount, err
}
///////////////////////////////////////
///////////////////////////////////////
// CreateTemplate saves a new template to the server
func (client *Client) CreateTemplate(template Template) (TemplateInfo, error) {
res := TemplateInfo{}
err := client.doRequest(parameters{
Method: "POST",
Path: "templates",
Payload: template,
TokenType: server_token,
}, &res)
return res, err
}
///////////////////////////////////////
///////////////////////////////////////
// EditTemplate updates details for a specific template with templateID
func (client *Client) EditTemplate(templateID string, template Template) (TemplateInfo, error) {
res := TemplateInfo{}
err := client.doRequest(parameters{
Method: "PUT",
Path: fmt.Sprintf("templates/%s", templateID),
Payload: template,
TokenType: server_token,
}, &res)
return res, err
}
///////////////////////////////////////
///////////////////////////////////////
// DeleteTemplate removes a template (with templateID) from the server
func (client *Client) DeleteTemplate(templateID string) error {
res := APIError{}
err := client.doRequest(parameters{
Method: "DELETE",
Path: fmt.Sprintf("templates/%s", templateID),
TokenType: server_token,
}, &res)
if res.ErrorCode != 0 {
return res
}
return err
}
///////////////////////////////////////
///////////////////////////////////////
// ValidateTemplateBody contains the template/render model combination to be validated
type ValidateTemplateBody struct {
Subject string
TextBody string
HTMLBody string `json:"HtmlBody"`
TestRenderModel map[string]interface{}
InlineCSSForHTMLTestRender bool `json:"InlineCssForHtmlTestRender"`
}
// ValidateTemplateResponse contains information as to how the validation went
type ValidateTemplateResponse struct {
AllContentIsValid bool
HTMLBody Validation `json:"HtmlBody"`
TextBody Validation
Subject Validation
SuggestedTemplateModel map[string]interface{}
}
// Validation contains the results of a field's validation
type Validation struct {
ContentIsValid bool
ValidationErrors []ValidationError
RenderedContent string
}
// ValidationError contains information about the errors which occurred during validation for a given field
type ValidationError struct {
Message string
Line int
CharacterPosition int
}
// ValidateTemplate validates the provided template/render model combination
func (client *Client) ValidateTemplate(validateTemplateBody ValidateTemplateBody) (ValidateTemplateResponse, error) {
res := ValidateTemplateResponse{}
err := client.doRequest(parameters{
Method: "POST",
Path: "templates/validate",
Payload: validateTemplateBody,
TokenType: server_token,
}, &res)
return res, err
}
///////////////////////////////////////
///////////////////////////////////////
// TemplatedEmail is used to send an email via a template
type TemplatedEmail struct {
// TemplateId: REQUIRED if TemplateAlias is not specified. - The template id to use when sending this message.
TemplateId int64 `json:",omitempty"`
// TemplateAlias: REQUIRED if TemplateId is not specified. - The template alias to use when sending this message.
TemplateAlias string `json:",omitempty"`
// TemplateModel: The model to be applied to the specified template to generate HtmlBody, TextBody, and Subject.
TemplateModel map[string]interface{} `json:",omitempty"`
// InlineCss: By default, if the specified template contains an HTMLBody, we will apply the style blocks as inline attributes to the rendered HTML content. You may opt-out of this behavior by passing false for this request field.
InlineCss bool `json:",omitempty"`
// From: The sender email address. Must have a registered and confirmed Sender Signature.
From string `json:",omitempty"`
// To: REQUIRED Recipient email address. Multiple addresses are comma separated. Max 50.
To string `json:",omitempty"`
// Cc recipient email address. Multiple addresses are comma separated. Max 50.
Cc string `json:",omitempty"`
// Bcc recipient email address. Multiple addresses are comma separated. Max 50.
Bcc string `json:",omitempty"`
// Tag: Email tag that allows you to categorize outgoing emails and get detailed statistics.
Tag string `json:",omitempty"`
// Reply To override email address. Defaults to the Reply To set in the sender signature.
ReplyTo string `json:",omitempty"`
// Headers: List of custom headers to include.
Headers []Header `json:",omitempty"`
// TrackLinks: Activate link tracking for links in the HTML or Text bodies of this email.
// Possible options: None HtmlAndText HtmlOnly TextOnly
TrackLinks TrackLinksOption `json:",omitempty"`
// TrackOpens: Activate open tracking for this email.
TrackOpens bool `json:",omitempty"`
// Attachments: List of attachments
Attachments []Attachment `json:",omitempty"`
}
// SendTemplatedEmail sends an email using a template (TemplateId)
func (client *Client) SendTemplatedEmail(email TemplatedEmail) (EmailResponse, error) {
res := EmailResponse{}
err := client.doRequest(parameters{
Method: "POST",
Path: "email/withTemplate",
Payload: email,
TokenType: server_token,
}, &res)
return res, err
}
// SendTemplatedEmail sends batch email using a template (TemplateId)
func (client *Client) SendTemplatedEmailBatch(emails []TemplatedEmail) ([]EmailResponse, error) {
res := []EmailResponse{}
var formatEmails map[string]interface{} = map[string]interface{}{
"Messages": emails,
}
err := client.doRequest(parameters{
Method: "POST",
Path: "email/batchWithTemplates",
Payload: formatEmails,
TokenType: server_token,
}, &res)
return res, err
}