forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
227 lines (200 loc) · 7.27 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
package main
// This is an example of the Mail helper, located here: /helpers/mail
import (
"fmt"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
//"../../.." // to test against the downloaded version
//"../../../../sendgrid-go/helpers/mail" // to test against the downloaded version
"os"
)
// Minimum required to send an email
func helloEmail() []byte {
address := "test@example.com"
name := "Example User"
from := mail.NewEmail(name, address)
subject := "Hello World from the SendGrid Go Library"
address = "test@example.com"
name = "Example User"
to := mail.NewEmail(name, address)
content := mail.NewContent("text/plain", "some text here")
m := mail.NewV3MailInit(from, subject, to, content)
address = "test2@example.com"
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 := "test@example.com"
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", "test1@example.com"),
mail.NewEmail("Example User", "test2@example.com"),
}
p.AddTos(tos...)
ccs := []*mail.Email{
mail.NewEmail("Example User", "test3@example.com"),
mail.NewEmail("Example User", "test4@example.com"),
}
p.AddCCs(ccs...)
bccs := []*mail.Email{
mail.NewEmail("Example User", "test5@example.com"),
mail.NewEmail("Example User", "test6@example.com"),
}
p.AddBCCs(bccs...)
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", "test1@example.com"),
mail.NewEmail("Example User", "test2@example.com"),
}
p2.AddTos(tos2...)
ccs2 := []*mail.Email{
mail.NewEmail("Example User", "test3@example.com"),
mail.NewEmail("Example User", "test4@example.com"),
}
p2.AddCCs(ccs2...)
bccs = []*mail.Email{
mail.NewEmail("Example User", "test5@example.com"),
mail.NewEmail("Example User", "test6@example.com"),
}
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")
a.SetContentID("Balance Sheet")
m.AddAttachment(a)
a2 := mail.NewAttachment()
a2.SetContent("BwdW")
a2.SetType("image/png")
a2.SetFilename("banner.png")
a2.SetDisposition("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("test@example.com")
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", "test@example.com")
m.SetReplyTo(replyToEmail)
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 main() {
sendHelloEmail()
sendKitchenSink()
}