-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsmtp_service.go
302 lines (251 loc) · 8.55 KB
/
smtp_service.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package sendpulse_sdk_go
import (
"context"
b64 "encoding/base64"
"fmt"
"net/http"
"strings"
"time"
)
type SmtpService struct {
client *Client
}
func newSmtpService(cl *Client) *SmtpService {
return &SmtpService{client: cl}
}
type EmailTemplate struct {
ID string `json:"id"`
Variables map[string]any `json:"variables"`
}
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
type SendEmailParams struct {
Html string `json:"html,omitempty"`
Text string `json:"text,omitempty"`
Template *EmailTemplate `json:"template"`
AutoPlainText bool `json:"auto_plain_text"`
Subject string `json:"subject"`
From User `json:"from"`
To []User `json:"to"`
Attachments map[string]string `json:"attachments"`
}
func (service *SmtpService) SendMessage(ctx context.Context, params SendEmailParams) (string, error) {
path := "/smtp/emails"
type paramsFormat struct {
Email SendEmailParams `json:"email"`
}
if params.Html != "" {
html := b64.StdEncoding.EncodeToString([]byte(params.Html))
params.Html = html
}
data := paramsFormat{Email: params}
var response struct {
Result bool `json:"result"`
ID string `json:"id"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &response, true)
return response.ID, err
}
type SmtpMessage struct {
ID string `json:"id"`
Sender string `json:"sender"`
TotalSize int `json:"total_size"`
SenderIP string `json:"sender_ip"`
SmtpAnswerCode int `json:"smtp_answer_code"`
SmtpAnswerCodeExplain string `json:"smtp_answer_code_explain"`
SmtpAnswerSubcode string `json:"smtp_answer_subcode"`
SmtpAnswerData string `json:"smtp_answer_data"`
UsedIP string `json:"used_ip"`
Recipient string `json:"recipient"`
Subject string `json:"subject"`
SendDate DateTime `json:"send_date"`
Tracking struct {
Click int `json:"click"`
Open int `json:"open"`
Link []*struct {
Url string `json:"url"`
Browser string `json:"browser"`
Os string `json:"os"`
ScreenResolution string `json:"screen_resolution"`
IP string `json:"ip"`
ActionDate DateTime `json:"action_date"`
} `json:"link"`
ClientInfo []*struct {
Browser string `json:"browser"`
Os string `json:"os"`
IP string `json:"ip"`
ActionDate DateTime `json:"action_date"`
} `json:"client_info"`
} `json:"tracking"`
}
type SmtpListParams struct {
Limit int
Offset int
From time.Time
To time.Time
Sender string
Recipient string
}
func (service *SmtpService) GetMessages(ctx context.Context, params SmtpListParams) ([]*SmtpMessage, error) {
path := "/smtp/emails"
var urlParts []string
urlParts = append(urlParts, fmt.Sprintf("offset=%d", params.Offset))
if params.Limit != 0 {
urlParts = append(urlParts, fmt.Sprintf("limit=%d", params.Limit))
}
if !params.From.IsZero() {
urlParts = append(urlParts, fmt.Sprintf("from=%s", params.From.Format("2006-01-02")))
}
if !params.To.IsZero() {
urlParts = append(urlParts, fmt.Sprintf("to=%s", params.From.Format("2006-01-02")))
}
if params.Sender != "" {
urlParts = append(urlParts, fmt.Sprintf("sender=%s", params.Sender))
}
if params.Recipient != "" {
urlParts = append(urlParts, fmt.Sprintf("recipient=%s", params.Recipient))
}
if len(urlParts) != 0 {
path += "?" + strings.Join(urlParts, "&")
}
var respData []*SmtpMessage
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmtpService) CountMessages(ctx context.Context) (int, error) {
path := "/smtp/emails/total"
var respData struct {
Total int `json:"total"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Total, err
}
func (service *SmtpService) GetMessage(ctx context.Context, id int) (*SmtpMessage, error) {
path := fmt.Sprintf("/smtp/emails/%d", id)
var respData *SmtpMessage
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
type BouncesList struct {
Total int `json:"total"`
Emails []struct {
EmailTo string `json:"email_to"`
Sender string `json:"sender"`
SendDate DateTime `json:"send_date"`
Subject string `json:"subject"`
SmtpAnswerCode int `json:"smtp_answer_code"`
SmtpAnswerSubcode string `json:"smtp_answer_subcode"`
SmtpAnswerData string `json:"smtp_answer_data"`
} `json:"emails"`
RequestLimit int `json:"request_limit"`
Found int `json:"found"`
}
func (service *SmtpService) GetDailyBounces(ctx context.Context, limit, offset int, date time.Time) (*BouncesList, error) {
path := fmt.Sprintf("/smtp/bounces/day?limit=%d&offset=%d", limit, offset)
if !date.IsZero() {
path += "&date=" + date.Format("2006-01-02")
}
var respData *BouncesList
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmtpService) CountBounces(ctx context.Context) (int, error) {
path := "/smtp/bounces/day/total"
var respData struct {
Total int `json:"total"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Total, err
}
type SmtpUnsubscribeEmail struct {
Email string `json:"email"`
Comment string `json:"comment"`
}
func (service *SmtpService) UnsubscribeEmails(ctx context.Context, emails []*SmtpUnsubscribeEmail) error {
path := "/smtp/unsubscribe"
type paramsFormat struct {
Emails []*SmtpUnsubscribeEmail `json:"emails"`
}
data := paramsFormat{Emails: emails}
var respData struct {
Result bool `json:"true"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return err
}
func (service *SmtpService) DeleteUnsubscribedEmails(ctx context.Context, emails []string) error {
path := "/smtp/unsubscribe"
type paramsFormat struct {
Emails []string `json:"emails"`
}
data := paramsFormat{Emails: emails}
var respData struct {
Result bool `json:"true"`
}
_, err := service.client.newRequest(ctx, http.MethodDelete, path, data, &respData, true)
return err
}
type UnsubscribedListParams struct {
Limit int
Offset int
Date time.Time
}
type Unsubscribed struct {
Email string `json:"email"`
UnsubscribeByLink int `json:"unsubscribe_by_link"`
UnsubscribeByUser int `json:"unsubscribe_by_user"`
SpamComplaint int `json:"spam_complaint"`
Date DateTime `json:"date"`
}
func (service *SmtpService) GetUnsubscribedEmails(ctx context.Context, params UnsubscribedListParams) ([]Unsubscribed, error) {
path := fmt.Sprintf("/smtp/unsubscribe?offset=%d", params.Offset)
if params.Limit != 0 {
path += fmt.Sprintf("&limit=%d", params.Limit)
}
if !params.Date.IsZero() {
path += "&date=" + params.Date.Format("2006-01-02")
}
var respData []Unsubscribed
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmtpService) GetSendersIPs(ctx context.Context) ([]string, error) {
path := "/smtp/ips"
var respData []string
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmtpService) GetSendersEmails(ctx context.Context) ([]string, error) {
path := "/smtp/senders"
var respData []string
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmtpService) GetAllowedDomains(ctx context.Context) ([]string, error) {
path := "/smtp/domains"
var respData []string
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmtpService) AddDomain(ctx context.Context, email string) error {
path := "/smtp/domains"
type data struct {
Email string `json:"email"`
}
var respData struct {
Result bool `json:"result"`
}
params := data{Email: email}
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return err
}
func (service *SmtpService) VerifyDomain(ctx context.Context, email string) error {
path := fmt.Sprintf("/domains/%s", email)
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return err
}