Skip to content

Commit d1ee6ad

Browse files
author
Kaylyn Sigler
authored
Merge pull request #44 from tariq1890/master
Adding Usage.md for smtpapi-go
2 parents 60ab84e + 9883f39 commit d1ee6ad

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed

USAGE.md

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
# Usage
2+
3+
## Initialisation
4+
5+
```go
6+
package main
7+
8+
import (
9+
"fmt"
10+
"github.com/sendgrid/smtpapi-go"
11+
)
12+
13+
header := smtpapi.NewSMTPAPIHeader()
14+
```
15+
16+
## Table of Contents
17+
18+
- [Recipients](#recipients)
19+
- [Categories](#categories)
20+
- [Substitution](#substitution)
21+
- [Sections](#sections)
22+
- [Filters](#filters)
23+
- [Advanced Suppression Manager (ASM)](#asm)
24+
- [Send At](#send-at)
25+
- [Batches](#batches)
26+
- [IP Pools](#ip-pools)
27+
- [Retrieving Data](#retrieving-data)
28+
29+
<a name="recipients"></a>
30+
## Recipients
31+
32+
#### Adding a single recipient
33+
34+
The `header.AddTo` method allows you to add a new recipient to the `To` array.
35+
36+
To do this, you can use the function with an email address, and optionally include the recipient's name as a second parameter.
37+
38+
```go
39+
header.SetTo("[email protected]") // An email address with no name provided
40+
header.SetTo("[email protected]", "An Example User") // An email address with a name provided as the second parameter
41+
```
42+
43+
#### Setting multiple recipients
44+
45+
The `header.SetTos` method allows you to set multiple recipients by providing an array of email addresses.
46+
47+
This will set the `To` array to the array you provide. This will need to either be an array of emails, or if names are provided, they need to be formatted as `{{ name }} <{{ email }}>`.
48+
49+
```go
50+
header.SetTos([]string{
51+
"[email protected]", // An email address with no name
52+
"An Example User <[email protected]>", // An email with a name field
53+
})
54+
```
55+
56+
<a name="categories"></a>
57+
## Categories
58+
59+
Categories are a useful way to organise your email analytics by tagging your emails with a specific type or topic.
60+
61+
There are multiple methods available for setting your categories when sending emails.
62+
63+
#### Adding a single category
64+
65+
The `header.AddCategory` method can be used to add a category to your list.
66+
67+
```go
68+
header.AddCategory('marketing') // Add a new category of 'marketing' to the array of categories
69+
```
70+
71+
#### Setting multiple categories
72+
73+
The `header.SetCategories` method can be used to set your categories list to an array of strings.
74+
75+
This is useful when there are a set amount of categories required for the email you are sending.
76+
77+
This method will remove any categories that have been previously set.
78+
79+
```go
80+
header.SetCategories([]string{
81+
"marketing",
82+
"sales",
83+
}) // Sets the current categories to be 'marketing' and 'sales'
84+
```
85+
86+
#### Setting a single category
87+
88+
The `header.SetCategory` method can be used to set a single specific category.
89+
90+
It is useful for removing previously set categories. And will create a new array with the string you provide.
91+
92+
This method will remove any categories that have been previously set.
93+
94+
```go
95+
header.SetCategory("marketing") // Reset the categories to be 'marketing' only
96+
```
97+
98+
<a name="substitution"></a>
99+
## Substitution
100+
101+
Substitutions are a great way of writing short dynamic email content easily,
102+
103+
#### Adding a single substitution string
104+
105+
The `header.AddSubstitution` method can be used to replace content for recipients.
106+
107+
```go
108+
header.AddSubstitution("-name-", "John") // Replace the -name- variable with John.
109+
```
110+
111+
#### Setting substitution strings
112+
113+
The `header.SetSubstitutions` method can be used to replace content for any number of strings.
114+
115+
This method will reset any key pairs that have previously been set.
116+
117+
```go
118+
header.SetSubstitutions(map[string][]string{
119+
"-name-": {"John", "Jane"}, // Replace the -name- variable to John or Jane
120+
"-number-": {"555.555.5555", "777.777.7777"}, // Replace the -number- variable with the provided numbers
121+
})
122+
```
123+
124+
<a name="sections"></a>
125+
## Sections
126+
127+
Sections are similar to substitutions, but are specific to the actual message rather than the recipient.
128+
129+
This is useful when you are sending multiple emails with the same style, but different content.
130+
131+
Note that substitution variables can also be included within a section, but section variables cannot
132+
133+
#### Adding a section
134+
135+
The `header.AddSection` method can be used to add a new section to the sections array. This is useful for building up a list of sections dynamically, perhaps based on a user's actions.
136+
137+
```go
138+
header.AddSection("-event_details-", "The event will be held tomorrow.") // Replaces -event_details- with the event's string
139+
```
140+
141+
#### Setting multiple sections
142+
143+
The `header.SetSections` allows you to set multiple sections in a single array.
144+
145+
This is good when sending out multiple emails where no dynamic variation is required.
146+
147+
This will reset any section key-pairs that have previously been set.
148+
149+
```go
150+
header.SetSections(map[string]string{
151+
"-event_details-": "The event will be held tomorrow.",
152+
"-event_open_time-": "'It will be open from 1am to 9pm.",
153+
})
154+
```
155+
156+
<a name="filters"></a>
157+
## Filters
158+
159+
Filters allow you to dynamically toggle features such as click tracking, blind copying and DKIM domain validation.
160+
161+
#### Adding a single filter
162+
163+
Adding a filter is easy with the `header.AddFilter` method.
164+
165+
This method requires 3 values:
166+
- The filter's name
167+
- The parameter's name
168+
- The value
169+
170+
```go
171+
header.AddFilter("dkim", "use_from", true)
172+
header.AddFilter("dkim", "domain", "example.com")
173+
```
174+
175+
#### Adding pre-existing filters
176+
177+
Filters with predetermined settings can also be added using the `header.SetFilter` method.
178+
179+
```go
180+
filter := &Filter{
181+
Settings: make(map[string]interface{}),
182+
}
183+
filter.Settings["enable"] = 1
184+
filter.Settings["text/plain"] = "You can haz footers!"
185+
header.SetFilter("footer", filter)
186+
```
187+
188+
<a name="asm"></a>
189+
## Advanced Suppression Management (ASM)
190+
191+
Advanced Suppression Management (or Unsubscribe Groups) are a good way of allowing recipients to unsubscribe from a specific set of emails.
192+
193+
You can
194+
195+
#### Setting the ASM group ID
196+
197+
The `header.SetASMGroupID` method is a quick way to set the type of email that you are sending.
198+
199+
All it requires is the ID of the ASM group, which can be found using the API.
200+
201+
```go
202+
header.SetASMGroupID(42) // Sets the ASM ID to 42
203+
```
204+
205+
<a name="send-at"></a>
206+
## Send At
207+
208+
Scheduling the time of your email campaign can be done using a collection of quick and easy methods.
209+
210+
#### Adding a single 'send at' date
211+
212+
The `header.AddSendEachAt` method is a good way to add the time to send at.
213+
214+
This method requires a unix timestamp as the input.
215+
216+
```go
217+
header.AddSendEachAt(1508694645)
218+
```
219+
220+
#### Setting multiple 'send at' date
221+
222+
The `header.SetSendEachAt` method is useful for setting an array of times that recipients have their emails sent.
223+
224+
This method requires an array of unix timestamps as the input.
225+
226+
```go
227+
header.SetSendEachAt([]int64{
228+
1508694645,
229+
1508694835,
230+
})
231+
```
232+
233+
#### Setting a single date to send all emails
234+
235+
The `header.SetSendAt` method is useful for setting a single time that all emails in the collection will be sent.
236+
237+
This method requires a unix timestamp as the input.
238+
239+
```go
240+
header.SetSendAt(1508694645)
241+
```
242+
243+
<a name="batches"></a>
244+
## Batches
245+
246+
Batches are a great way to group a collection of scheduled items for sending. It allows you to cancel scheduled emails, and provides more control over the emails.
247+
248+
The batch ID can be set using the `header.AddBatchId` method. You must have generated the batch ID first through the API.
249+
250+
```go
251+
header.AddBatchId("HkJ5yLYULb7Rj8GKSx7u025ouWVlMgAi") // Adds a previously generated batch ID to the emails
252+
```
253+
254+
<a name="ip-pools"></a>
255+
## IP Pools
256+
257+
IP Pools allow you to group SendGrid IP addresses together. For example, if you have a set of marketing IPs, you can assign them a pool ID of `marketing`.
258+
259+
The IP Pool name can be set using the `header.SetIpPool` method. You must have generated the IP Pool first through the API.
260+
261+
```go
262+
header.SetIpPool("marketing") // Sets the IP Pool to be the marketing collection of IPs
263+
```
264+
265+
266+
<a name="retrieving-data"></a>
267+
#### Retrieving data as a JSON string
268+
269+
The `header.JSONString` method allows the data from the header instance to be exported as a JSON string.
270+
271+
```go
272+
headerString, _ := header.JSONString()
273+
fmt.Println(headerString)
274+
275+
// {"to":["[email protected]"],"sub":{"key":["value"]},"section":{"section":"value"},"category":["category"],"unique_args":{"key":"value"},"filters":{"filter":{"settings":{"setting":"value"}}},"asm_group_id":1,"send_at":1428611024,"ip_pool":"testPool"}
276+
```

0 commit comments

Comments
 (0)