-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendemail.go
322 lines (286 loc) · 11.4 KB
/
sendemail.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package mailchimp
import (
"context"
"net/http"
"time"
"github.com/carlmjohnson/errorx"
"github.com/carlmjohnson/requests"
"github.com/carlmjohnson/resperr"
"github.com/spotlightpa/almanack/pkg/almlog"
)
func (v3 V3) SendEmail(ctx context.Context, subject, body string) (err error) {
l := almlog.FromContext(ctx)
var mcErr ErrorResponse
defer func() {
if err != nil {
err = resperr.New(http.StatusBadGateway,
"MailChimp [%3d] %s: %s: %q: %w",
mcErr.Status, mcErr.Title, mcErr.Detail, mcErr.Errors, err,
)
}
}()
defer errorx.Trace(&err)
var res PostCampaignResponse
if err = requests.
New(v3.config).
Path("campaigns").
BodyJSON(PostCampaignRequest{
Type: "plaintext",
Recipients: Recipients{ListID: v3.listID},
Settings: Settings{
FromName: "Spotlight PA",
ReplyTo: "press@spotlightpa.org",
SubjectLine: subject,
Title: subject,
},
}).
ErrorJSON(&mcErr).
ToJSON(&res).
Fetch(ctx); err != nil {
return err
}
l.InfoContext(ctx, "mailchimp.SendEmail: created campaign", "campaign_id", res.ID)
var putRes PutCampaignResponse
if err = requests.
New(v3.config).
Put().
Pathf("campaigns/%s/content", res.ID).
BodyJSON(PutCampaignRequest{
PlainText: body,
}).
ErrorJSON(&mcErr).
ToJSON(&putRes).
Fetch(ctx); err != nil {
return err
}
l.InfoContext(ctx, "mailchimp.SendEmail: configured campaign", "campaign_id", res.ID)
if err = requests.
New(v3.config).
Pathf("campaigns/%s/actions/send", res.ID).
BodyBytes(nil).
ErrorJSON(&mcErr).
Fetch(ctx); err != nil {
return err
}
l.InfoContext(ctx, "mailchimp.SendEmail: sent campaign", "campaign_id", res.ID)
return nil
}
type ErrorResponse struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Instance string `json:"instance"`
Errors []any `json:"errors"`
}
type PostCampaignRequest struct {
Type string `json:"type"`
Recipients Recipients `json:"recipients,omitempty"`
Settings Settings `json:"settings,omitempty"`
VariateSettings *VariateSettings `json:"variate_settings,omitempty"`
Tracking *Tracking `json:"tracking,omitempty"`
RssOpts *RssOpts `json:"rss_opts,omitempty"`
SocialCard *SocialCard `json:"social_card,omitempty"`
ContentType string `json:"content_type,omitempty"`
}
type PostCampaignResponse struct {
ID string `json:"id"`
WebID int `json:"web_id"`
ParentCampaignID string `json:"parent_campaign_id"`
Type string `json:"type"`
CreateTime time.Time `json:"create_time"`
ArchiveURL string `json:"archive_url"`
LongArchiveURL string `json:"long_archive_url"`
Status string `json:"status"`
EmailsSent int `json:"emails_sent"`
SendTime string `json:"send_time"`
ContentType string `json:"content_type"`
NeedsBlockRefresh bool `json:"needs_block_refresh"`
Resendable bool `json:"resendable"`
Recipients Recipients `json:"recipients"`
Settings Settings `json:"settings"`
VariateSettings VariateSettings `json:"variate_settings"`
Tracking Tracking `json:"tracking"`
RssOpts RssOpts `json:"rss_opts"`
AbSplitOpts AbSplitOpts `json:"ab_split_opts"`
SocialCard SocialCard `json:"social_card"`
ReportSummary ReportSummary `json:"report_summary"`
DeliveryStatus DeliveryStatus `json:"delivery_status"`
Links []Links `json:"_links"`
}
type SegmentOpts struct {
SavedSegmentID int `json:"saved_segment_id,omitempty"`
PrebuiltSegmentID string `json:"prebuilt_segment_id,omitempty"`
Match string `json:"match,omitempty"`
Conditions []any `json:"conditions,omitempty"`
}
type Recipients struct {
ListID string `json:"list_id,omitempty"`
ListIsActive bool `json:"list_is_active,omitempty"`
ListName string `json:"list_name,omitempty"`
SegmentText string `json:"segment_text,omitempty"`
RecipientCount int `json:"recipient_count,omitempty"`
SegmentOpts SegmentOpts `json:"segment_opts,omitempty"`
}
type Settings struct {
SubjectLine string `json:"subject_line,omitempty"`
PreviewText string `json:"preview_text,omitempty"`
Title string `json:"title,omitempty"`
FromName string `json:"from_name,omitempty"`
ReplyTo string `json:"reply_to,omitempty"`
UseConversation bool `json:"use_conversation,omitempty"`
ToName string `json:"to_name,omitempty"`
FolderID string `json:"folder_id,omitempty"`
Authenticate bool `json:"authenticate,omitempty"`
AutoFooter bool `json:"auto_footer,omitempty"`
InlineCSS bool `json:"inline_css,omitempty"`
AutoTweet bool `json:"auto_tweet,omitempty"`
AutoFbPost []string `json:"auto_fb_post,omitempty"`
FbComments bool `json:"fb_comments,omitempty"`
Timewarp bool `json:"timewarp,omitempty"`
TemplateID int `json:"template_id,omitempty"`
DragAndDrop bool `json:"drag_and_drop,omitempty"`
}
type Combinations struct {
ID string `json:"id,omitempty"`
SubjectLine int `json:"subject_line,omitempty"`
SendTime int `json:"send_time,omitempty"`
FromName int `json:"from_name,omitempty"`
ReplyTo int `json:"reply_to,omitempty"`
ContentDescription int `json:"content_description,omitempty"`
Recipients int `json:"recipients,omitempty"`
}
type VariateSettings struct {
WinningCombinationID string `json:"winning_combination_id,omitempty"`
WinningCampaignID string `json:"winning_campaign_id,omitempty"`
WinnerCriteria string `json:"winner_criteria,omitempty"`
WaitTime int `json:"wait_time,omitempty"`
TestSize int `json:"test_size,omitempty"`
SubjectLines []string `json:"subject_lines,omitempty"`
SendTimes []time.Time `json:"send_times,omitempty"`
FromNames []string `json:"from_names,omitempty"`
ReplyToAddresses []string `json:"reply_to_addresses,omitempty"`
Contents []string `json:"contents,omitempty"`
Combinations []Combinations `json:"combinations,omitempty"`
}
type Salesforce struct {
Campaign bool `json:"campaign,omitempty"`
Notes bool `json:"notes,omitempty"`
}
type Capsule struct {
Notes bool `json:"notes,omitempty"`
}
type Tracking struct {
Opens bool `json:"opens,omitempty"`
HTMLClicks bool `json:"html_clicks,omitempty"`
TextClicks bool `json:"text_clicks,omitempty"`
GoalTracking bool `json:"goal_tracking,omitempty"`
Ecomm360 bool `json:"ecomm360,omitempty"`
GoogleAnalytics string `json:"google_analytics,omitempty"`
Clicktale string `json:"clicktale,omitempty"`
Salesforce Salesforce `json:"salesforce,omitempty"`
Capsule Capsule `json:"capsule,omitempty"`
}
type DailySend struct {
Sunday bool `json:"sunday,omitempty"`
Monday bool `json:"monday,omitempty"`
Tuesday bool `json:"tuesday,omitempty"`
Wednesday bool `json:"wednesday,omitempty"`
Thursday bool `json:"thursday,omitempty"`
Friday bool `json:"friday,omitempty"`
Saturday bool `json:"saturday,omitempty"`
}
type Schedule struct {
Hour int `json:"hour,omitempty"`
DailySend DailySend `json:"daily_send,omitempty"`
WeeklySendDay string `json:"weekly_send_day,omitempty"`
MonthlySendDate int `json:"monthly_send_date,omitempty"`
}
type RssOpts struct {
FeedURL string `json:"feed_url,omitempty"`
Frequency string `json:"frequency,omitempty"`
Schedule Schedule `json:"schedule,omitempty"`
LastSent time.Time `json:"last_sent,omitempty"`
ConstrainRssImg bool `json:"constrain_rss_img,omitempty"`
}
type AbSplitOpts struct {
SplitTest string `json:"split_test,omitempty"`
PickWinner string `json:"pick_winner,omitempty"`
WaitUnits string `json:"wait_units,omitempty"`
WaitTime int `json:"wait_time,omitempty"`
SplitSize int `json:"split_size,omitempty"`
FromNameA string `json:"from_name_a,omitempty"`
FromNameB string `json:"from_name_b,omitempty"`
ReplyEmailA string `json:"reply_email_a,omitempty"`
ReplyEmailB string `json:"reply_email_b,omitempty"`
SubjectA string `json:"subject_a,omitempty"`
SubjectB string `json:"subject_b,omitempty"`
SendTimeA time.Time `json:"send_time_a,omitempty"`
SendTimeB time.Time `json:"send_time_b,omitempty"`
SendTimeWinner string `json:"send_time_winner,omitempty"`
}
type SocialCard struct {
ImageURL string `json:"image_url,omitempty"`
Description string `json:"description,omitempty"`
Title string `json:"title,omitempty"`
}
type Ecommerce struct {
TotalOrders int `json:"total_orders,omitempty"`
TotalSpent int `json:"total_spent,omitempty"`
TotalRevenue int `json:"total_revenue,omitempty"`
}
type ReportSummary struct {
Opens int `json:"opens,omitempty"`
UniqueOpens int `json:"unique_opens,omitempty"`
OpenRate int `json:"open_rate,omitempty"`
Clicks int `json:"clicks,omitempty"`
SubscriberClicks int `json:"subscriber_clicks,omitempty"`
ClickRate int `json:"click_rate,omitempty"`
Ecommerce Ecommerce `json:"ecommerce,omitempty"`
}
type DeliveryStatus struct {
Enabled bool `json:"enabled,omitempty"`
CanCancel bool `json:"can_cancel,omitempty"`
Status string `json:"status,omitempty"`
EmailsSent int `json:"emails_sent,omitempty"`
EmailsCanceled int `json:"emails_canceled,omitempty"`
}
type Links struct {
Rel string `json:"rel,omitempty"`
Href string `json:"href,omitempty"`
Method string `json:"method,omitempty"`
TargetSchema string `json:"targetSchema,omitempty"`
Schema string `json:"schema,omitempty"`
}
type PutCampaignRequest struct {
PlainText string `json:"plain_text,omitempty"`
HTML string `json:"html,omitempty"`
URL string `json:"url,omitempty"`
Template *Template `json:"template,omitempty"`
Archive *Archive `json:"archive,omitempty"`
VariateContents []any `json:"variate_contents,omitempty"`
}
type Template struct {
ID int `json:"id,omitempty"`
Sections map[string]string `json:"sections,omitempty"`
}
type Archive struct {
ArchiveContent string `json:"archive_content,omitempty"`
ArchiveType string `json:"archive_type,omitempty"`
}
type PutCampaignResponse struct {
VariateContents []VariateContents `json:"variate_contents"`
PlainText string `json:"plain_text"`
HTML string `json:"html"`
ArchiveHTML string `json:"archive_html"`
Links []Links `json:"_links"`
}
type VariateContents struct {
ContentLabel string `json:"content_label"`
PlainText string `json:"plain_text"`
HTML string `json:"html"`
}
type TestEmailRequest struct {
TestEmails []string `json:"test_emails"`
SendType string `json:"send_type"`
}