forked from mailgun/mailgun-js-boland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailgun.js
597 lines (511 loc) · 19.5 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*!
* mailgun-js
* Copyright(c) 2012, 2013 OneLobby <bojan@onelobby.com>
* MIT Licensed
*/
var https = require('https');
var qs = require('querystring');
var noop = function () {
};
/**
* Initializes the Mailgun module
* @param api_key {String} the API Key for the mailgun account
* @param domain {String} the mailgun domain
* @type {Function}
*/
module.exports = function (api_key, domain) {
if (!api_key) {
throw new Error('Mailgun "API key" required');
}
if (!domain) {
throw new Error('Mailgun "domain" required');
}
var username = 'api';
var host = 'api.mailgun.net';
var endpoint = '/v2';
var auth = [username, api_key].join(':');
/**
* The main function that does all the work. The client really shouldn't call this.
* @param method {String} HTTP method
* @param resource {String} the resource
* @param data {Object} the data to be sent in the request
* @param cb {Function} callback for the request
* @return
*/
function _request(method, resource, data, cb) {
if (typeof data === 'function' && !cb) {
cb = data;
data = {};
}
if (!cb) cb = noop;
var getDomain = function () {
var d = '/' + domain;
//filter out API calls that do not require a domain specified
if ((resource.indexOf('routes') >= 0)
|| (resource.indexOf('lists') >= 0)
|| (resource.indexOf('domains') >= 0 )) {
d = '';
}
return d;
};
var path = ''.concat(endpoint, getDomain(), resource);
var qsdata = qs.stringify(data);
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': qsdata.length
};
var opts = {
hostname: host,
path: path,
method: method,
headers: headers,
auth: auth,
agent: false
};
var req = https.request(opts, function (res) {
var chunks = '';
var error;
res.on('data', function (chunk) {
chunks += chunk;
});
res.on('error', function (err) {
error = err;
});
res.on('end', function () {
if (error) {
return cb(error, res);
}
if (res && (res.headers['content-type'].indexOf('application/json') == 0)) {
try {
var body = JSON.parse(chunks);
if (!error && res.statusCode !== 200) {
error = new Error(body.message);
}
}
catch (e) {
}
}
return cb(error, res, body);
});
});
req.on('error', function (e) {
return cb(e);
});
req.write(qsdata);
req.end();
}
function _post(path, data, callback) {
return _request('POST', path, data, callback);
}
function _get(path, data, callback) {
return _request('GET', path, data, callback);
}
function _del(path, data, callback) {
return _request('DELETE', path, data, callback);
}
function _put(path, data, callback) {
return _request('PUT', path, data, callback);
}
return {
/**
* Expose helper methods to allow users to interact with parts of the api that
* are not exposed already.
*
* Example
* mailgun.get('/stats',function(err,stats){
* console.log(stats)
* })
*/
post: _post,
get: _get,
del: _del,
put: _put,
messages: {
/**
* Creates a new email message and sends it using mailgun
* @param data {Object} the object containing the data to be sent.
* Required parameters are 'to', 'from', and 'text' or 'html'
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
send: function (data, cb) {
if (!data.to) {
return (cb || noop)(new Error('You must include a "to" parameter.'));
}
if (!data.from) {
return (cb || noop)(new Error('You must include a "from" parameter.'));
}
if (!data.text && !data.html) {
return (cb || noop)(new Error('You must include a "text" or "html" parameter.'));
}
return _post('/messages', data, cb);
}
},
mailboxes: {
/**
* Lists mailboxes associated with the mailgun account
* @param data {Object} the optional object containing the GET options 'limit' and 'skip'
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
list: function (data, cb) {
return _get('/mailboxes', data, cb);
},
/**
* Creates a new mailbox associated with the mailgun account
* @param data {Object} the object containing the new mailbox name ('mailbox') and new password ('password')
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
create: function (data, cb) {
if (!data.mailbox) {
return (cb || noop)(new Error('You must include name of the mailbox in the \'mailbox\' parameter.'));
}
if (!data.password) {
return (cb || noop)(new Error('You must include a password for the new mailbox.'));
}
return _post('/mailboxes', data, cb);
},
/**
* Deletes a mailbox associated with the mailgun account
* @param mailbox {String} the mailbox to be deleted
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
del: function (mailbox, cb) {
if (!mailbox || typeof mailbox !== 'string') {
return (cb || noop)(new Error('You must include name of the mailbox.'));
}
return _del('/mailboxes/' + mailbox, cb);
},
/**
* Updates the mailbox associated with the mailgun account
* @param data {Object} the object containing the mailbox name ('mailbox') and new password ('password')
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
update: function (data, cb) {
if (!data.mailbox) {
return (cb || noop)(new Error('You must include name of the mailbox in the \'mailbox\' parameter.'));
}
if (!data.password) {
return (cb || noop)(new Error('You must include a password for the mailbox.'));
}
var mailbox = data.mailbox;
delete data.mailbox;
return _put('/mailboxes/' + mailbox, data, cb);
}
},
routes: {
/**
* Lists routes associated with the mailgun account
* @param data {Object} the optional object containing the GET options 'limit' and 'skip'
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
list: function (data, cb) {
return _get('/routes', data, cb);
},
/**
* Returns a single route object based on its ID.
* @param id {String} the route ID
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
get: function (id, cb) {
if (!id || typeof id !== 'string') {
return (cb || noop)(new Error('You must include id of the route.'));
}
return _get('/routes/' + id, cb);
},
/**
* Creates a new route
* @param data {Object} the object containing the priority, description, expression and action as strings
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
create: function (data, cb) {
if (!data.expression) {
return (cb || noop)(new Error('You must include route expression.'));
}
if (!data.action) {
return (cb || noop)(new Error('You must include route action.'));
}
return _post('/routes', data, cb);
},
/**
* Updates a given route by ID. All data parameters optional:
* this API call only updates the specified fields leaving others unchanged.
* @param id {String} the route ID
* @param data {Object} the object containing the priority, description, expression and action as strings
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
update: function (id, data, cb) {
if (!data || (typeof data !== 'object')) {
return (cb || noop)(new Error('You must include data.'));
}
if (!id || typeof id !== 'string') {
return cb(new Error('You must include id of the route.'));
}
return _put('/routes/' + id, data, cb);
},
/**
* Deletes a route based on the id.
* @param id {String} the id of the route to be deleted
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
del: function (id, cb) {
if (!id || typeof id !== 'string') {
return (cb || noop)(new Error('You must include the id of the route.'));
}
return _del('/routes/' + id, cb);
}
},
lists: {
/**
* Returns a list of mailing lists under your account.
* @param data {Object} the optional object containing the GET options 'address', 'limit' and 'skip'
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
list: function (data, cb) {
return _get('/lists', data, cb);
},
/**
* Returns a single mailing list by a given address.
* @param address {String} the mailing list address
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
get: function (address, cb) {
if (!address || typeof address !== 'string') {
return (cb || noop)(new Error('You must include mailing list address.'));
}
return _get('/lists/' + address, cb);
},
/**
* Creates a new mailing list.
* @param data {Object} the object containing the address, name, description, and access_level as strings
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
create: function (data, cb) {
if (!data.address) {
return (cb || noop)(new Error('You must include mailing list address.'));
}
return _post('/lists', data, cb);
},
/**
* Update mailing list properties, such as address, description or name.
* @param address {String} mailing list address
* @param data {Object} the object containing the address, name, description, and access_level as strings
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
update: function (address, data, cb) {
if (!data || (typeof data !== 'object')) {
return (cb || noop)(new Error('You must include data.'));
}
if (!address || typeof address !== 'string') {
return (cb || noop)(new Error('You must include mailing list address.'));
}
return _put('/lists/' + address, data, cb);
},
/**
* Deletes a mailing list.
* @param address {String} mailing list address
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
del: function (address, cb) {
if (!address || typeof address !== 'string') {
return (cb || noop)(new Error('You must include mailing list address.'));
}
return _del('/lists/' + address, cb);
},
/**
* Fetches mailing list stats.
* @param address {String} mailing list address
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
stats: function (address, cb) {
if (!address || typeof address !== 'string') {
return (cb || noop)(new Error('You must include mailing list address.'));
}
return _get('/lists/' + address + '/stats', cb);
},
members: {
/**
* Fetches the list of mailing list members.
* @param listAddress {String} mailing list address
* @param data {Object} the optional object containing the GET options 'subscribed', 'limit' and 'skip'
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
list: function (listAddress, data, cb) {
if (!listAddress || typeof listAddress !== 'string') {
var last = arguments.length - 1;
var callback = (arguments[last] && (typeof arguments[last] === 'function')) ? arguments[last] : Function;
return callback(new Error('You must include mailing list address.'));
}
return _get('/lists/' + listAddress + '/members', data, cb);
},
/**
* Retrieves a mailing list member.
* @param listAddress {String} mailing list address
* @param memberAddress {String} member address
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
get: function (listAddress, memberAddress, cb) {
var last = arguments.length - 1;
var callback = (arguments[last] && (typeof arguments[last] === 'function')) ? arguments[last] : Function;
if (!listAddress || typeof listAddress !== 'string') {
return callback(new Error('You must include mailing list address.'));
}
if (!memberAddress || typeof memberAddress !== 'string') {
return callback(new Error('You must include member address.'));
}
return _get('/lists/' + listAddress + '/members/' + memberAddress, cb);
},
/**
* Adds a member to the mailing list.
* @param listAddress {String} mailing list address
* @param data {Object} the object containing the address, name, vars, subscribed and upsert
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
create: function (listAddress, data, cb) {
var last = arguments.length - 1;
var callback = (arguments[last] && (typeof arguments[last] === 'function')) ? arguments[last] : Function;
if (!data || (typeof data !== 'object')) {
return callback(new Error('You must include data.'));
}
if (!listAddress || typeof listAddress !== 'string') {
return callback(new Error('You must include mailing list address.'));
}
if (!data.address || typeof data.address !== 'string') {
return callback(new Error('You must include member address.'));
}
var params = {};
// prepare vars, has to be valid JSON
if (data.vars && typeof data.vars === 'object') {
for (var key in data) {
params[key] = data[key];
}
params.vars = JSON.stringify(params.vars);
}
else {
params = data;
}
return _post('/lists/' + listAddress + '/members', params, cb);
},
/**
* Updates a mailing list member with given properties. Won’t touch the property if it’s not passed in.
* @param listAddress {String} mailing list address
* @param memberAddress {String} member address
* @param data {Object} the object containing the address, name, vars, subscribed
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
update: function (listAddress, memberAddress, data, cb) {
var last = arguments.length - 1;
var callback = (arguments[last] && (typeof arguments[last] === 'function')) ? arguments[last] : Function;
if (!listAddress || typeof listAddress !== 'string') {
return callback(new Error('You must include mailing list address.'));
}
if (!memberAddress || typeof memberAddress !== 'string') {
return callback(new Error('You must include member address.'));
}
if (!data || (typeof data !== 'object')) {
return callback(new Error('You must include data to update.'));
}
var params = {};
// prepare vars, has to be valid JSON
if (data.vars && typeof data.vars === 'object') {
for (var key in data) {
params[key] = data[key];
}
params.vars = JSON.stringify(params.vars);
}
else {
params = data;
}
return _put('/lists/' + listAddress + '/members/' + memberAddress, params, cb);
},
/**
* Delete a mailing list member.
* @param listAddress {String} mailing list address
* @param memberAddress {String} member address
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
del: function (listAddress, memberAddress, cb) {
var last = arguments.length - 1;
var callback = (arguments[last] && (typeof arguments[last] === 'function')) ? arguments[last] : Function;
if (!listAddress || typeof listAddress !== 'string') {
return callback(new Error('You must include mailing list address.'));
}
if (!memberAddress || typeof memberAddress !== 'string') {
return callback(new Error('You must include member address.'));
}
return _del('/lists/' + listAddress + '/members/' + memberAddress, cb);
}
}
},
domains: {
/**
* Returns a list of domains under your account.
* @param data {Object} the optional object containing the GET options 'address', 'limit' and 'skip'
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
list: function (data, cb) {
return _get('/domains', data, cb);
},
/**
* Returns a single domain, including credentials and DNS records.
* @param domain {String} the domain name
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
get: function (domain, cb) {
if (!domain || typeof domain !== 'string') {
return (cb || noop)(new Error('You must include domain name.'));
}
return _get('/domains/' + domain, cb);
},
/**
* Create a new domain.
* @param data {Object} the object containing the domain name, smtp_password, spam_action and wildcard.
* name, smtp_password, and spam_action are string. wildcard boolean.
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
create: function (data, cb) {
if (!data.name) {
return (cb || noop)(new Error('You must include domain name.'));
}
if (!data.smtp_password) {
return (cb || noop)(new Error('You must include SMTP password.'));
}
return _post('/domains', data, cb);
},
/**
* Delete a domain from your account.
* @param domain {String} domain name
* @param cb {Function} callback function accepting error, response and body
* @type {Function}
*/
del: function (domain, cb) {
if (!domain || typeof domain !== 'string') {
return (cb || noop)(new Error('You must include domain name.'));
}
return _del('/domains/' + domain, cb);
}
}
};
};