-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbulk_send_complex.rb
More file actions
228 lines (190 loc) · 8.68 KB
/
bulk_send_complex.rb
File metadata and controls
228 lines (190 loc) · 8.68 KB
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
require_relative "../../lib/socketlabs-injectionapi.rb"
require "json"
class BulkSendComplex
include SocketLabs::InjectionApi
include SocketLabs::InjectionApi::Core
include SocketLabs::InjectionApi::Message
def get_message
message = BulkMessage.new
message.subject = "Sending A Complex Test Message (Bulk Send))"
message.html_body ="<html>" +
" <head><title>Sending A Complex Test Message</title></head>" +
" <body>" +
" <h1>Sending A Complex Test Message</h1>" +
" <h2>Merge Data</h2>" +
" <p>" +
" Motto = <b>%%Motto%%</b> </br>" +
" Birthday = <b>%%Birthday%%</b> </br>" +
" Age = <b>%%Age%%</b> </br>" +
" UpSell = <b>%%UpSell%%</b>" +
" </p>" +
" <h2>Example of Merge Usage</h2>" +
" <p>" +
" Our company motto is '<b>%%Motto%%</b>'. </br>" +
" Your birthday is <b>%%Birthday%%</b> and you are <b>%%Age%%</b> years old." +
" </p>" +
" <h2>UTF-8 Characters:</h2>" +
" <p>✔ - Check</p>" +
" <h2>Embedded Image:</h2>" +
" <p><img src='cid:bus' /></p>" +
" </body>" +
"</html>"
message.plain_text_body = "Sending A Complex Test Message" +
" Merged Data" +
" Motto = %%Motto%%" +
" Birthday = %%Birthday%%" +
" Age = %%Age%%" +
" UpSell = %%UpSell%%" +
" " +
" Example of Merge Usage" +
" Our company motto is '%%Motto%%'." +
" Your birthday is %%Birthday%% and you are %%Age%% years old."
message.amp_body = "<!doctype html>" +
"<html amp4email>" +
"<head>" +
"<title>Sending an AMP Test Message</title>" +
" <meta charset=\"utf-8\">"+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>" +
" <style amp4email-boilerplate>body{visibility:hidden}</style>" +
" <style amp-custom>" +
" h1 {" +
" margin: 1rem;" +
" }" +
" </style>" +
"</head>" +
"<body>" +
" <h1>Sending An AMP Complex Test Message</h1>" +
" <h2>Merge Data</h2>" +
" <p>" +
" Motto = <b>%%Motto%%</b> </br>" +
" Birthday = <b>%%Birthday%%</b> </br>" +
" Age = <b>%%Age%%</b> </br>" +
" UpSell = <b>%%UpSell%%</b>" +
" </p>" +
" <h2>Example of Merge Usage</h2>" +
" <p>" +
" Our company motto is '<b>%%Motto%%</b>'. </br>" +
" Your birthday is <b>%%Birthday%%</b> and you are <b>%%Age%%</b> years old." +
" </p>" +
" <h2>UTF-8 Characters:</h2>" +
" <p>✔ - Check</p>" +
" </body>" +
" </html>"
message.message_id = "ComplexExample"
message.mailing_id = "BulkSend"
message.charset = "UTF-8"
message.from_email_address = EmailAddress.new("from@example.com","FromMe")
message.reply_to_email_address = EmailAddress.new("replyto@example.com")
# Add some global merge-data
# (These will be applied to all Recipients unless specifically overridden by Recipient level merge data)
# --------
# Add global merge data using a Array
global_merge_data = Array.new
# EmailAddress object in array
global_merge_data.push(MergeData.new("Birthday", "unknown"))
# hash in array
global_merge_data.push({ :key => "Motto", :value => "When hitting the inbox matters!" })
message.global_merge_data = global_merge_data
# Add Email Addresses directly to the Array
# EmailAddress object in array
message.global_merge_data.push(MergeData.new("Age", "an unknown number of"))
# hash in array
message.global_merge_data.push({ :key => "This wont show.", :value => "No Text" })
# Add global merge data using the add_global_merge_data function
message.add_global_merge_data("UpSell", "BTW: You are eligible for discount pricing when you upgrade your service!")
# Add recipients with merge data
# Including merge data on the recipient with the same name as the global merge data will override global merge data
# --------
# Add recipients with merge data using an Array
rec1_merge_data = Array.new
rec1_merge_data.push(MergeData.new("Birthday", "08/05/1991"))
rec1_merge_data.push(MergeData.new("Age", "27"))
rec1 = BulkRecipient.new("recipient1@example.com", { :merge_data => rec1_merge_data })
message.to_recipient.push(rec1)
# Add recipients with merge data using the add_to_recipient function
rec2_merge_data = Array.new
rec2_merge_data.push(MergeData.new("Birthday", "04/12/1984"))
rec2_merge_data.push(MergeData.new("UpSell", ""))
rec2_merge_data.push({ key: "Age", value: "34" })
message.add_to_recipient("recipient2@example.com", "Recipient #2", rec2_merge_data)
# Add recipients with merge data directly to the Array using a Hash
message.to_recipient.push({
:email_address => "recipient3@example.com",
:friendly_name => "Recipient 3",
:merge_data => [
{ :key => "Birthday", :value => "10/30/1978" },
{ :key => "UpSell", :value => "" },
{ :key => "Age", :value => "40" }
]
})
# Add recipients using the addToRecipient function
# The merge data for this Recipient will be populated with Global merge data
message.add_to_recipient("recipient4@example.com", "Recipient #4")
# Adding Custom Headers
# --------
# Add CustomHeader using a list
headers = Array.new
headers.push(CustomHeader.new("example-type", "basic-send-complex-example"))
headers.push(CustomHeader.new("message-contains", "attachments, headers"))
message.custom_headers = headers
# Add CustomHeader directly to the list
message.custom_headers.push(CustomHeader.new("message-has-attachments", "true"))
# Add CustomHeader using the add_custom_header function
message.add_custom_header("testMessageHeader", "I am a message header")
message.add_custom_header(CustomHeader.new("testMessageHeader2", "I am another message header"))
# Adding Attachments
attachment1 = Attachment.new(
name:"bus",
file_path:"../img/bus.png",
mime_type:"image/png"
)
message.attachments.push(attachment1)
# Add Attachment using the add_attachment function
attachment2 = Attachment.new(
name:"bus2.png",
file_path:"../img/bus.png",
mime_type:"image/png"
)
attachment2.content_id = "bus"
message.add_attachment(attachment2)
# Add Attachment a filePath {string} to the array
message.add_attachment(Attachment.new(file_path:"../html/SampleEmail.html"))
# Add Attachment using bytes of the file
file_path = "../img/bus.png"
file = File.open(file_path, "rb")
data = Base64.encode64(file.read)
attachment4 = Attachment.new(
name: "yellow-bus.png",
mime_type: "image/png",
content: data
)
# Add CustomHeaders to Attachment
attachment4.custom_headers.push(CustomHeader.new("Color", "Yellow"))
attachment4.add_custom_header("Place", "Beach")
message.add_attachment(attachment4)
# Adding Metadata
# --------
# Add Metadata using a list
metadata = Array.new
metadata.push(Metadata.new("x-mycustommetadata", "I am custom metadata"))
metadata.push(Metadata.new("x-internalid", "123"))
message.metadata = metadata
# Add Metadata directly to the list
message.metadata.push(Metadata.new("x-mycustommetadata", "I am custom metadata"))
# Add Metadata using the add_metadata function
message.add_metadata("x-mycustommetadata2", "Custom metadata")
message.add_metadata(Metadata.new("x-externalid", "456"))
# Adding Tags
# --------
# Add Tags using a list
tags = Array.new
tags.push("example-type:bulk-send-complex")
message.tags = tags
# Add Tags directly to the list
message.metadata.push("has-attachments:true")
# Add Metadata using the add_metadata function
message.add_tag("I am a test message")
message.add_tag("ruby-Example")
message
end
end