-
Notifications
You must be signed in to change notification settings - Fork 342
/
send_grid_helper.ex
349 lines (269 loc) · 9.52 KB
/
send_grid_helper.ex
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
defmodule Bamboo.SendGridHelper do
@moduledoc """
Functions for using features specific to Sendgrid.
## Example
email
|> with_template("80509523-83de-42b6-a2bf-54b7513bd2aa")
|> substitute("%name%", "Jon Snow")
|> substitute("%location%", "Westeros")
"""
alias Bamboo.Email
@field_name :send_grid_template
@categories :categories
@asm_group_id :asm_group_id
@bypass_list_management :bypass_list_management
@google_analytics_enabled :google_analytics_enabled
@google_analytics_utm_params :google_analytics_utm_params
@additional_personalizations :additional_personalizations
@allowed_google_analytics_utm_params ~w(utm_source utm_medium utm_campaign utm_term utm_content)a
@send_at_field :sendgrid_send_at
@ip_pool_name_field :ip_pool_name
@unique_args :unique_args
@click_tracking_enabled :click_tracking_enabled
@doc """
Specify the template for SendGrid to use for the context of the substitution
tags.
## Example
email
|> with_template("80509523-83de-42b6-a2bf-54b7513bd2aa")
"""
def with_template(email, template_id) do
template = Map.get(email.private, @field_name, %{})
email
|> Email.put_private(@field_name, set_template(template, template_id))
end
@doc """
Add a tag to the list of substitutions in the SendGrid template.
The tag must be a `String.t` due to SendGrid using special characters to wrap
tags in the template.
## Example
email
|> substitute("%name%", "Jon Snow")
"""
def substitute(email, tag, value) do
if is_binary(tag) do
template = Map.get(email.private, @field_name, %{})
email
|> Email.put_private(@field_name, add_substitution(template, tag, value))
else
raise "expected the tag parameter to be of type binary, got #{tag}"
end
end
@doc """
Sets a list of categories for this email.
A maximum of 10 categories can be assigned to an email. Duplicate categories will
be ignored and only unique entries will be sent.
## Example
email
|> with_categories("campaign-12345")
"""
def with_categories(email, categories) when is_list(categories) do
categories =
(Map.get(email.private, @categories, []) ++ categories)
|> MapSet.new()
|> MapSet.to_list()
email
|> Email.put_private(@categories, Enum.slice(categories, 0, 10))
end
def with_categories(_email, _categories) do
raise "expected a list of category strings"
end
@doc """
Add a property to the list of dynamic template data in the SendGrid template.
This will be added to the request as:
```
"personalizations":[
{
"to":[
{
"email":"example@sendgrid.net"
}
],
"dynamic_template_data":{
"total":"$ 239.85",
}
}
],
```
The tag can be of any type since SendGrid allows you to use Handlebars in its templates
## Example
email
|> add_dynamic_field("name", "Jon Snow")
"""
def add_dynamic_field(email, field, value) when is_atom(field),
do: add_dynamic_field(email, Atom.to_string(field), value)
def add_dynamic_field(email, field, value) when is_binary(field) do
template = Map.get(email.private, @field_name, %{})
email
|> Email.put_private(@field_name, add_dynamic_field_to_template(template, field, value))
end
def add_dynamic_field(_email, field, _value),
do: raise("expected the name parameter to be of type binary or atom, got #{field}")
@doc """
Set the ASM (Advanced Suppression Manager) group that this email should belong to.
This can be used to let recipients unsubscribe from only a certain type of communication.
## Example
email
|> with_asm_group_id(1234)
"""
def with_asm_group_id(email, asm_group_id) when is_integer(asm_group_id) do
email
|> Email.put_private(@asm_group_id, asm_group_id)
end
def with_asm_group_id(_email, asm_group_id) do
raise "expected the asm_group_id parameter to be an integer, got #{asm_group_id}"
end
@doc """
Instruct SendGrid to bypass list management for this email.
If enabled, SendGrid will ignore any email supression (such as
unsubscriptions, bounces, spam filters) for this email. This is useful for
emails that all users must receive, such as Terms of Service updates, or
password resets.
## Example
email
|> with_bypass_list_management(true)
"""
def with_bypass_list_management(email, enabled) when is_boolean(enabled) do
email
|> Email.put_private(@bypass_list_management, enabled)
end
def with_bypass_list_management(_email, enabled) do
raise "expected bypass_list_management parameter to be a boolean, got #{enabled}"
end
@doc """
Instruct SendGrid to enable or disable Google Analytics tracking, and
optionally set the UTM parameters for it.
This is useful if you need to control UTM tracking parameters on an individual email
basis.
## Example
email
|> with_google_analytics(true, %{utm_source: "email", utm_campaign: "campaign"})
email
|> with_google_analytics(false)
"""
def with_google_analytics(email, enabled, utm_params \\ %{})
def with_google_analytics(email, enabled, utm_params)
when is_boolean(enabled) do
utm_params =
utm_params
|> Map.take(@allowed_google_analytics_utm_params)
email
|> Email.put_private(@google_analytics_enabled, enabled)
|> Email.put_private(@google_analytics_utm_params, utm_params)
end
def with_google_analytics(_email, _enabled, _utm_params) do
raise "expected with_google_analytics enabled parameter to be a boolean"
end
@doc """
Instruct SendGrid to enable or disable Click Tracking for a particular email.
Read more about SendGrid click tracking [here](https://docs.sendgrid.com/ui/account-and-settings/tracking#click-tracking)
## Example
email
|> with_click_tracking(true)
email
|> with_click_tracking(false)
"""
def with_click_tracking(email, enabled)
when is_boolean(enabled),
do: Email.put_private(email, @click_tracking_enabled, enabled)
def with_click_tracking(_email, _enabled) do
raise "expected with_click_tracking enabled parameter to be a boolean"
end
@doc """
Schedule a time for SendGrid to deliver the email.
Note that if the time is in the past, SendGrid will immediately deliver the
email.
## Example
{:ok, delivery_time, _} = DateTime.from_iso8601("2020-01-01T00:00:00Z")
email
|> with_send_at(delivery_time)
"""
@spec with_send_at(%Email{}, %DateTime{} | integer()) :: %Email{}
def with_send_at(email, %DateTime{} = time) do
timestamp = DateTime.to_unix(time)
email
|> Email.put_private(@send_at_field, timestamp)
end
def with_send_at(email, unix_timestamp) when is_integer(unix_timestamp) do
email
|> Email.put_private(@send_at_field, unix_timestamp)
end
def with_send_at(_email, _time) do
raise "expected with_send_at time parameter to be a DateTime or unix timestamp"
end
@doc """
Add SendGrid personalizations.
Each personalization can have the following fields: `to`, `cc`, `bcc`,
`subject`, `headers`, `substitutions`, `custom_args`, or `send_at`.
Settings from the top level of the email (e.g., `Email |> with_send_at`)
will not be applied to each personalization. If you want multiple
personalizations with common properties, it is recommended to generate the
list from a common base value and simply do not set the corresponding
top-level fields.
## Example:
base_personalization = %{
bcc: [%{"email" => "bcc@bar.com", "name" => "BCC"}],
subject: "Here is your email"
}
personalizations =
Enum.map(
[
%{to: "one@test.com"},
%{to: "two@test.com", send_at: 1_580_485_560}
],
&Map.merge(base_personalization, &1)
)
email =
new_email()
|> Email.put_header("Reply-To", "reply@foo.com")
|> Bamboo.SendGridHelper.add_personalizations(personalizations)
"""
@spec add_personalizations(Bamboo.Email.t(), [map]) :: Bamboo.Email.t()
def add_personalizations(email, personalizations) when is_list(personalizations) do
email
|> Email.put_private(@additional_personalizations, personalizations)
end
defp set_template(template, template_id) do
template
|> Map.merge(%{template_id: template_id})
end
defp add_substitution(template, tag, value) do
template
|> Map.update(:substitutions, %{tag => value}, fn substitutions ->
Map.merge(substitutions, %{tag => value})
end)
end
defp add_dynamic_field_to_template(template, field, value) do
template
|> Map.update(:dynamic_template_data, %{field => value}, fn dynamic_data ->
Map.merge(dynamic_data, %{field => value})
end)
end
@doc """
Specify the ip pool name.
## Example
email
|> with_ip_pool_name("my-ip-pool-name")
"""
def with_ip_pool_name(email, ip_pool_name) do
email
|> Email.put_private(@ip_pool_name_field, ip_pool_name)
end
@doc """
Set a map of unique arguments for this email.
This will override any existing unique arguments.
## Example
email
|> with_unique_args(%{new_arg_1: "new arg 1", new_arg_2: "new arg 2"})
"""
def with_unique_args(email, unique_args) when is_map(unique_args) do
unique_args =
Map.get(email.private, @unique_args, %{})
|> Map.merge(unique_args)
email
|> Email.put_private(@unique_args, unique_args)
end
def with_unique_args(_email, _unique_args) do
raise "expected a map of unique arguments"
end
end