-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbulkSendComplexExample.js
More file actions
211 lines (184 loc) · 6.79 KB
/
bulkSendComplexExample.js
File metadata and controls
211 lines (184 loc) · 6.79 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
const { SocketLabsClient, EmailAddress, BulkMessage, BulkRecipient, Attachment, CustomHeader, MergeData, Metadata } = require('../../src/socketlabsClient');
const exampleConfig = require('../exampleConfig');
const resolve = require('path').resolve;
/**
* Build the message
*/
var message = new BulkMessage();
message.messageId = "ComplexExample";
message.mailingId = "BulkSend";
message.charSet = "UTF-8";
message.subject = "Sending A Complex Test Message (Bulk Send)";
// Build the Content (Note the %% symbols used to denote the data to be merged)
var html = "<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>"
+ " </body>"
+ "</html>";
message.htmlBody = html
var text = "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.textBody = text;
var amp = "<!doctype html>"
+ "<html amp4email>"
+ "<head>"
+ " <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>This is the AMP Html Body of my 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.ampBody = amp;
message.from = new EmailAddress("from@example.com");
message.replyTo = new EmailAddress("replyto@example.com");
/**
* Adding To global merge data
* (These will be applied to all Recipients unless specifically overridden by Recipient level merge data)
*/
// Add global merge data using an Array
var globalMergeData = [];
globalMergeData.push(new MergeData("Birthday", "Unknown"));
globalMergeData.push({ key: "Age", value: "an unknown number of" });
message.globalMergeData = globalMergeData;
// Add global merge data directly to the Array
message.globalMergeData.push(new MergeData("Motto", "When hitting the Inbox matters!"));
// Add global merge data using the addGlobalMergeData function
message.addGlobalMergeData("UpSell", "BTW: You are eligible for discount pricing when you upgrade your service!")
/**
* Adding To Recipients
* 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
var rec1MergeData = [];
rec1MergeData.push(new MergeData("Birthday", "08/05/1991"));
rec1MergeData.push(new MergeData("Age", "27"));
var rec1 = new BulkRecipient("recipient1@example.com", { mergeData: rec1MergeData });
message.to.push(rec1);
// Add recipients with merge data directly to the Array
var rec2MergeData = [];
rec2MergeData.push(new MergeData("Birthday", "04/12/1984"));
rec2MergeData.push(new MergeData("UpSell", ""));
rec2MergeData.push({ key: "Age", value: "34" });
message.addToRecipient("recipient2@example.com", "Recipient #2", rec2MergeData);
// Add recipients with merge data directly to the Array
message.to.push({
emailAddress: "recipient3@example.com",
friendlyName: "Recipient #3",
mergeData: [
{ 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.addToRecipient("recipient4@example.com", "Recipient #4");
/**
* Adding Attachments
*/
// Add Attachment directly to the Array
var attachment1 = new Attachment({
name: "bus.png",
filePath: resolve("../img/bus.png"),
contentType: "image/png"
});
message.attachments.push(attachment1);
// Add Attachment a filePath {string} to the array
message.attachments.push(resolve("../html/sampleemail.html"));
/**
* Adding Custom Headers
*/
// Add Custom Headers using an Array
var headers = [];
headers.push(new CustomHeader("example-type", "bulk-send-complex"));
headers.push({ name: "message-contains", value: "merge data, attachments, headers" });
message.customHeaders = headers;
// Add Custom Headers directly to the Array
message.customHeaders.push(new CustomHeader("message-has-attachments", "true"));
// Add Custom Headers using the addCustomHeaders function
message.addCustomHeaders("testMessageHeader", "I am a message header");
/**
* Adding Metadata
*/
// Add Metadata using an Array
var metadata = [];
metadata.push(new Metadata("example-type", "basic-send-complex"));
metadata.push({ name: "message-contains", value: "attachments, headers" });
message.metadata = metadata;
// Add Metadata directly to the Array
message.metadata.push(new Metadata("message-has-attachments", "true"));
// Add Metadata using the addCustomHeaders function
message.addMetadata("testMessageHeader", "I am metadata");
/**
* Adding Tags
*/
// Add Tags using an Array
var tags = [];
tags.push("example-type:bulk-send-complex");
message.tags = tags;
// Add Tags directly to the Array
message.tags.push("has-attachments:true");
// Add Tags using the addCustomHeaders function
message.addTag("I am a test message");
message.addTag("nodejs-Example");
/**
* Create the client
*/
var client = new SocketLabsClient(exampleConfig.ServerId, exampleConfig.ApiKey);
/**
* Send the message
*/
client.send(message).then(
(res) => {
console.log("Promise resolved: ")
console.log(res)
},
(err) => {
console.log("Promise rejected: ")
console.log(err)
}
);