forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.go
311 lines (273 loc) · 9.76 KB
/
example.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
package main
// This is an example of the Mail helper, located here: /helpers/mail
import (
"fmt"
"os"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
// Minimum required to send an email
func helloEmail() []byte {
address := "[email protected]"
name := "Example User"
from := mail.NewEmail(name, address)
subject := "Hello World from the SendGrid Go Library"
address = "[email protected]"
name = "Example User"
to := mail.NewEmail(name, address)
content := mail.NewContent("text/plain", "some text here")
m := mail.NewV3MailInit(from, subject, to, content)
address = "[email protected]"
name = "Example User"
email := mail.NewEmail(name, address)
m.Personalizations[0].AddTos(email)
return mail.GetRequestBody(m)
}
// Fully populated Mail object
func kitchenSink() []byte {
m := mail.NewV3Mail()
address := "[email protected]"
name := "Example User"
e := mail.NewEmail(name, address)
m.SetFrom(e)
m.Subject = "Hello World from the SendGrid Go Library"
p := mail.NewPersonalization()
tos := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p.AddTos(tos...)
ccs := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p.AddCCs(ccs...)
bccs := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p.AddBCCs(bccs...)
from := mail.NewEmail("Example Sender", "[email protected]")
p.AddFrom(from)
p.Subject = "Hello World from the Personalized SendGrid Go Library"
p.SetHeader("X-Test", "test")
p.SetHeader("X-Mock", "true")
p.SetSubstitution("%name%", "Example User")
p.SetSubstitution("%city%", "Denver")
p.SetCustomArg("user_id", "343")
p.SetCustomArg("type", "marketing")
p.SetSendAt(1461356286)
m.AddPersonalizations(p)
p2 := mail.NewPersonalization()
tos2 := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p2.AddTos(tos2...)
ccs2 := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p2.AddCCs(ccs2...)
bccs = []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p2.AddBCCs(bccs...)
p2.Subject = "Hello World from the Personalized SendGrid Go Library"
p2.SetHeader("X-Test", "test")
p2.SetHeader("X-Mock", "true")
p2.SetSubstitution("%name%", "Example User")
p2.SetSubstitution("%city%", "Denver")
p2.SetCustomArg("user_id", "343")
p2.SetCustomArg("type", "marketing")
p2.SetSendAt(1461356286)
m.AddPersonalizations(p2)
c := mail.NewContent("text/plain", "some text here")
m.AddContent(c)
c = mail.NewContent("text/html", "some html here")
m.AddContent(c)
a := mail.NewAttachment()
a.SetContent("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")
a.SetType("application/pdf")
a.SetFilename("balance_001.pdf")
a.SetDisposition("attachment")
m.AddAttachment(a)
a2 := mail.NewAttachment()
a2.SetContent("BwdW")
a2.SetType("image/png")
a2.SetFilename("banner.png")
a2.SetDisposition("inline")
// Content-ID header is included when the attachment disposition is set to "inline"
a2.SetContentID("Banner")
m.AddAttachment(a2)
m.SetTemplateID("13b8f94f-bcae-4ec6-b752-70d6cb59f932")
m.AddSection("%section1%", "Substitution Text for Section 1")
m.AddSection("%section2%", "Substitution Text for Section 2")
m.SetHeader("X-Test1", "1")
m.SetHeader("X-Test2", "2")
m.AddCategories("May")
m.AddCategories("2016")
m.SetCustomArg("campaign", "welcome")
m.SetCustomArg("weekday", "morning")
m.SetSendAt(1461356286)
asm := mail.NewASM()
asm.SetGroupID(99)
asm.AddGroupsToDisplay(99)
m.SetASM(asm)
// This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work
// m.SetBatchID("sendgrid_batch_id")
m.SetIPPoolID("23")
mailSettings := mail.NewMailSettings()
bccSettings := mail.NewBCCSetting()
bccSettings.SetEnable(true)
bccSettings.SetEmail("[email protected]")
mailSettings.SetBCC(bccSettings)
sandBoxMode := mail.NewSetting(true)
mailSettings.SetSandboxMode(sandBoxMode)
bypassListManagement := mail.NewSetting(true)
mailSettings.SetBypassListManagement(bypassListManagement)
footerSetting := mail.NewFooterSetting()
footerSetting.SetText("Footer Text")
footerSetting.SetEnable(true)
footerSetting.SetHTML("<html><body>Footer Text</body></html>")
mailSettings.SetFooter(footerSetting)
spamCheckSetting := mail.NewSpamCheckSetting()
spamCheckSetting.SetEnable(true)
spamCheckSetting.SetSpamThreshold(1)
spamCheckSetting.SetPostToURL("https://spamcatcher.sendgrid.com")
mailSettings.SetSpamCheckSettings(spamCheckSetting)
m.SetMailSettings(mailSettings)
trackingSettings := mail.NewTrackingSettings()
clickTrackingSettings := mail.NewClickTrackingSetting()
clickTrackingSettings.SetEnable(true)
clickTrackingSettings.SetEnableText(true)
trackingSettings.SetClickTracking(clickTrackingSettings)
openTrackingSetting := mail.NewOpenTrackingSetting()
openTrackingSetting.SetEnable(true)
openTrackingSetting.SetSubstitutionTag("Optional tag to replace with the open image in the body of the message")
trackingSettings.SetOpenTracking(openTrackingSetting)
subscriptionTrackingSetting := mail.NewSubscriptionTrackingSetting()
subscriptionTrackingSetting.SetEnable(true)
subscriptionTrackingSetting.SetText("text to insert into the text/plain portion of the message")
subscriptionTrackingSetting.SetHTML("<html><body>html to insert into the text/html portion of the message</body></html>")
subscriptionTrackingSetting.SetSubstitutionTag("Optional tag to replace with the open image in the body of the message")
trackingSettings.SetSubscriptionTracking(subscriptionTrackingSetting)
googleAnalyticsSetting := mail.NewGaSetting()
googleAnalyticsSetting.SetEnable(true)
googleAnalyticsSetting.SetCampaignSource("some source")
googleAnalyticsSetting.SetCampaignTerm("some term")
googleAnalyticsSetting.SetCampaignContent("some content")
googleAnalyticsSetting.SetCampaignName("some name")
googleAnalyticsSetting.SetCampaignMedium("some medium")
trackingSettings.SetGoogleAnalytics(googleAnalyticsSetting)
m.SetTrackingSettings(trackingSettings)
replyToEmail := mail.NewEmail("Example User", "[email protected]")
m.SetReplyTo(replyToEmail)
return mail.GetRequestBody(m)
}
// Email utilizing dynamic transactional templates
// Note: you must customize subject line of the dynamic template itself
// Note: you may not use substitutions with dynamic templates
func dynamicTemplateEmail() []byte {
m := mail.NewV3Mail()
address := "[email protected]"
name := "Example User"
e := mail.NewEmail(name, address)
m.SetFrom(e)
m.SetTemplateID("d-c6dcf1f72bdd4beeb15a9aa6c72fcd2c")
p := mail.NewPersonalization()
tos := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
mail.NewEmail("Example User", "[email protected]"),
}
p.AddTos(tos...)
p.SetDynamicTemplateData("receipt", "true")
p.SetDynamicTemplateData("total", "$ 239.85")
items := []struct {
text string
image string
price string
}{
{
"New Line Sneakers",
"https://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.png",
"$ 79.95",
},
{
"Old Line Sneakers",
"https://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.png",
"$ 89.95",
},
{
"Blue Line Sneakers",
"https://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.png",
"$ 99.95",
},
}
var itemList []map[string]string
var item map[string]string
for _, v := range items {
item = make(map[string]string)
item["text"] = v.text
item["image"] = v.image
item["price"] = v.price
itemList = append(itemList, item)
}
p.SetDynamicTemplateData("items", itemList)
p.SetDynamicTemplateData("name", "Sample Name")
p.SetDynamicTemplateData("address01", "1234 Fake St.")
p.SetDynamicTemplateData("address02", "Apt. 123")
p.SetDynamicTemplateData("city", "Place")
p.SetDynamicTemplateData("state", "CO")
p.SetDynamicTemplateData("zip", "80202")
m.AddPersonalizations(p)
return mail.GetRequestBody(m)
}
func sendHelloEmail() {
request := sendgrid.GetRequest(os.Getenv("YOUR_SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
var Body = helloEmail()
request.Body = Body
response, err := sendgrid.API(request)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
func sendKitchenSink() {
request := sendgrid.GetRequest(os.Getenv("YOUR_SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
var Body = kitchenSink()
request.Body = Body
response, err := sendgrid.API(request)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
func sendDynamicTemplateEmail() {
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
var Body = dynamicTemplateEmail()
request.Body = Body
response, err := sendgrid.API(request)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
func main() {
sendHelloEmail()
sendKitchenSink()
sendDynamicTemplateEmail()
}