-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailgun.js
186 lines (172 loc) · 4.93 KB
/
mailgun.js
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
'use strict';
var contra = require('contra');
var mailgunjs = require('mailgun-js');
var addrs = require('email-addresses');
var inlineCss = require('inline-css');
var htmlToText = require('html-to-text');
var noKey = 'campaign-mailgun: API key not set';
function mailgun (options) {
if (!options) {
options = {};
}
if (!options.apiKey) {
warnNoKey();
return {
name: 'mailgun',
send: sendNoKey
};
}
return {
name: 'mailgun',
tweakPlaceholder: tweakPlaceholder,
send: send
};
function tweakPlaceholder (property, raw) {
return '%recipient.' + property + '%';
}
function warnNoKey () {
console.warn(noKey);
}
function sendNoKey (model, done) {
warnNoKey();
done(new Error(noKey));
}
function send (model, done) {
var provider = model.provider || {};
var providerTags = provider.tags || [];
var merge = provider.merge || {};
var domain = addrs.parseOneAddress(model.from).domain;
var authority = model.authority || options.authority
var client = mailgunjs({
apiKey: options.apiKey,
domain: domain
});
contra.concurrent({
html: inlineHtml,
images: getImages,
attachments: getAttachments,
}, ready);
function inlineHtml (next) {
var config = {
url: authority
};
inlineCss(model.html, config)
.then(function inlined (html) { next(null, html); })
.catch(function failed (err) { next(err); });
}
function getImages (next) {
var images = model.images ? model.images : [];
if (model._header) {
images.unshift({
name: '_header',
data: model._header.data,
mime: model._header.mime
});
}
next(null, images.map(transform));
function transform (image) {
return new client.Attachment({
data: new Buffer(image.data, 'base64'),
filename: image.name,
contentType: image.mime
});
}
}
function getAttachments (next) {
var attachments = model.attachments ? model.attachments : [];
next(null, attachments.map(transform));
function transform (attachment) {
return new client.Attachment({
data: attachment.file,
filename: attachment.name
});
}
}
function ready (err, result) {
if (err) {
done(err); return;
}
post(result.html, result.images, result.attachments);
}
function post (html, images, attachments) {
var inferConfig = {
wordwrap: 130,
linkHrefBaseUrl: authority,
hideLinkHrefIfSameAsText: true
};
var inferredText = htmlToText.fromString(html, inferConfig);
var tags = [model._template].concat(providerTags);
var batches = getRecipientBatches();
expandWildcard(model.to, model.cc, model.bcc);
contra.each(batches, 4, postBatch, responses);
function postBatch (batch, next) {
var req = {
from: model.from,
to: batch,
cc: model.cc,
bcc: model.bcc,
subject: model.subject,
html: html,
text: inferredText,
inline: images.slice(),
attachment: attachments.slice(),
'o:tag': tags.slice(),
'o:tracking': true,
'o:tracking-clicks': true,
'o:tracking-opens': true,
'recipient-variables': parseMergeVariables(batch, model.cc, model.bcc)
};
client.messages().send(req, next);
}
function responses (err, results) {
done(err, results);
}
}
function getRecipientBatches () {
var size = 250; // "Note: The maximum number of recipients allowed for Batch Sending is 1,000."
var batches = [];
for (var i = 0; i < model.to.length; i += size) {
batches.push(model.to.slice(i, i + size));
}
return batches;
}
function parseMergeVariables (to, cc, bcc) {
var variables = {};
to
.concat(cc)
.concat(bcc)
.forEach(addVariables);
return variables;
function addVariables (recipient) {
if (merge[recipient]) {
variables[recipient] = merge[recipient];
}
}
}
function expandWildcard (to, cc, bcc) {
if ('*' in merge) {
wildcarding();
}
function wildcarding () {
var wildcard = merge['*'];
to
.concat(cc)
.concat(bcc)
.forEach(addWildcardToRecipient);
function addWildcardToRecipient (recipient) {
Object.keys(wildcard).forEach(addWildcardKeyToRecipient);
function addWildcardKeyToRecipient (key) {
// don't override: wildcard has default values
if (!merge[recipient]) {
merge[recipient] = {};
}
if (!merge[recipient].hasOwnProperty(key)) {
merge[recipient][key] = wildcard[key];
}
}
}
}
}
}
}
module.exports = mailgun;