-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
584 lines (568 loc) · 24.6 KB
/
index.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
const Imap = require('imap');
const moment = require('moment');
const MailParser = require('mailparser').MailParser;
const nodemailer = require('nodemailer');
const htmlToText = require('html-to-text');
const crypto = require('crypto');
/**
Set of NodeJS functions to make working with emails easier. Please do not expect any sophisticated logic the main goal is to keep it simple and straightforward to use.
This module serves as an abstraction layer on top of several great modules for working different email protocols.
@module EmailTools
*/
function _getUid(from, to, date, subject) {
if(Array.isArray(date)) date = date[0];
if(Array.isArray(from)) from = from[0];
if(Array.isArray(to)) to = to[0];
if(Array.isArray(subject)) subject = subject[0];
to = typeof to !== "object" ? (to ? to.split(',')[0] : 'toUnknown') : to;
from = typeof from !== "object" ? (from ? from.split(',')[0] : 'fromUnknown') : from;
const momentDate = moment.utc(date).format("DD/MM/YYYY HH:mm:ss");
const fromAddress = typeof from === "object" ? from.address : from;
const toAddress = typeof to === "object" ? to.address : to;
const realFromAddress = fromAddress ? (fromAddress.indexOf('<') > -1 ? fromAddress.match(/<(.*?)>/i)[0]
.replace('<', '').replace('>', '') : fromAddress) : '';
const realToAddress = toAddress ? (toAddress.indexOf('<') > -1 ? toAddress.match(/<(.*?)>/i)[0]
.replace('<', '').replace('>', ''): toAddress) : '';
const uid = crypto.createHash('md5').update(
subject+
realFromAddress+
realToAddress+
momentDate
).digest('hex');
return uid;
}
module.exports.getUid = _getUid;
function normalizeFromAndTo(arr) {
var str = "";
if(arr && arr.length > 0) {
arr.forEach((entry,ix)=>{
str += entry.address;
if(ix < arr.length-1) str += ', ';
})
}
return str;
}
module.exports.normalizeFromAndTo = normalizeFromAndTo;
/**
* Internal function used to parse email message using MailParser
*
* @param {string} message - Message body
* @return {Promise} Resolved with object returned from MailParser
* @private
*/
function _parseMessage(message) {
return new Promise((resolve, reject)=>{
const mailParser = new MailParser();
mailParser.on("end", parsedMessage=>{
parsedMessage['fromString'] = normalizeFromAndTo(parsedMessage.from);
parsedMessage['toString'] = normalizeFromAndTo(parsedMessage.to);
parsedMessage['ccString'] = normalizeFromAndTo(parsedMessage.cc);
var header = Imap.parseHeader(message);
parsedMessage['uid'] = _getUid(parsedMessage.from, parsedMessage.to, header.date, header.subject);
resolve(parsedMessage)
});
mailParser.write(message);
mailParser.end();
})
}
/**
* All the text within %?% entries is replaced with a value from matching keys from vars
*
* @param {string} inputText Input text used as a template
* @param {Object} vars Variables used to replace template entries
* @return {string}
*/
module.exports.template = (inputText, vars) => {
if(inputText && typeof(inputText) === "string") {
return inputText.replace(new RegExp(Object.keys(vars).map(v=>{return '%'+v+'%'}).join("|"),"gm"), (m)=>{
return vars[m.replace(/%/g, '')];
});
}
return inputText;
};
/**
* Provides all the SMTP functions.
*/
module.exports.SMTP = {
/**
* Establish SMTP connection to host
*
* @param {Object} options - Connection options
* @param {string} options.user - SMTP account user
* @param {string} options.password - SMTP account password
* @param {string} options.host - SMTP host server
* @param {string} options.port - SMTP host server port
* @param {tls} options.tls - SMTP TLS connection flag
* @return {Object} SMTP connection instance as returned from [nodemailer]{@link https://github.com/nodemailer/nodemailer}
*/
connect: (options) => {
return nodemailer.createTransport({
host: options.host,
port: options.port,
secure: options.tls,
auth: {
user: options.user,
pass: options.password
}
});
},
/**
* Test SMTP connection
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.SMTP.connect} to establish the connection with host
* @return {Promise} Resolved on success and rejected on failure
*/
test: (options) => {
return new Promise((resolve, reject)=>{
const smtp = this.SMTP.connect(options.connection);
smtp.verify((err, success) => {
smtp.close();
if(err) {
reject(err)
} else {
resolve({status: 'OK'});
}
})
});
},
/**
* Send email
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.SMTP.connect} to establish the connection with host
* @param {Object} options.templateVars If provided the template() function is applied to Subject and HTML
* @param {Object} options.data Email message definition
* @param {string|Object} options.data.from Either string of sender or object having name and address attributes
* @param {string|Array} options.data.to Comma seperated or an Array of recipients
* @param {string|Array} options.data.cc Comma seperated or an Array of recipients
* @param {string|Array} options.data.bcc Comma seperated or an Array of recipients
* @param {string} options.data.subject The subject of the email
* @param {string} options.data.html Actual message send as html (automatically converted and added to a text field)
* @return {Promise} Resolved on success and rejected on failure
*/
send: (options) => {
return new Promise((resolve, reject)=>{
if(!options) reject(new Error("Options can't be empty"))
if(!options.data) reject(new Error("Options.Data can't be empty"))
const smtp = this.SMTP.connect(options.connection);
if(options.templateVars) {
options.data['html'] = this.template(options.data.html, options.templateVars);
options.data['subject'] = this.template(options.data.subject, options.templateVars);
}
options.data['text'] = htmlToText.fromString(options.data.html, {
ignoreImage: true
});
if(!options.data['date']) options.data['date'] = new Date();
smtp.sendMail(options.data, (err, info)=>{
smtp.close();
if(err){
reject(err);
} else {
info['date'] = options.data.date;
info['text'] = options.data.text;
info['html'] = options.data.html;
info['subject'] = options.data.subject;
resolve(info);
}
})
});
}
};
/**
* Provides all the IMAP functions.
*
* IMAP connection is established automatically upon every function call. This is as designed since nature
* of application this module was originally developed wouldn't allow to keep connection alive. This behaviour
* might be extended in the future to keep connection alive using a flag.
*
*/
module.exports.IMAP = {
_imapFetchHeadersHandler: fetch => {
return new Promise((resolve, reject)=>{
var messages = [];
fetch.once('error', fetchErr=>{
reject(fetchErr);
}).once('end', ()=>{
resolve(messages.sort((a,b)=>{
if (a.seqno > b.seqno)
return -1;
if (a.seqno < b.seqno)
return 1;
return 0;
}));
}).on('message', (msg, seqno)=>{
msg.on('body', (stream, info)=>{
var buffer = '';
stream.on('data', chunk=>{
buffer += chunk.toString('utf8');
}).once('end', ()=>{
var header = Imap.parseHeader(buffer);
header.uid = _getUid(header.from, header.to, header.date, header.subject);
header.seqno = seqno;
if(header && header.date) {
if(Array.isArray(header.date)) {
header.date = moment(new Date(header.date[0])).format('DD/MM/YY HH:mm');
} else {
header.date = moment(new Date(header.date)).format('DD/MM/YY HH:mm');
}
}
messages.push(header);
});
});
});
});
},
_imapConnectAndOpenBox: (options,cb, end) => {
if (!options.connection) {
end(new Error("Connection must be provided"));
} else if(!options.folderName) {
end(new Error("Folder name must be provided"));
} else {
const imap = this.IMAP.connect(options.connection);
var _err, _data;
imap.once('error', err=> {
imap.destroy();
end(err);
});
imap.once('ready', ()=> {
imap.openBox(options.folderName, options.folderReadonly ? true : false, (err, box)=> {
if (err) {
_err = err;
imap.end();
} else if(!box) {
_err = new Error("Unable to open folder" + options.folderName);
imap.end();
} else {
cb({imap: imap, box: box}, (err, data)=> {
_data = data;
_err = err;
imap.closeBox(err=> {
if(err && !_err) _err = err;
imap.end();
})
});
}
})
});
imap.once('end', () => {
end(_err, _data);
});
imap.connect();
}
},
/**
* Establish IMAP connection to host
*
* @param {Object} options - Connection options
* @param {string} options.user - IMAP account user
* @param {string} options.password - IMAP account password
* @param {string} options.host - IMAP host server
* @param {string} options.port - IMAP host server port
* @param {tls} options.tls - IMAP TLS connection flag
* @return {Imap} IMAP connection instance as returned from [node-imap]{@link https://github.com/mscdex/node-imap}
*/
connect: (options) => {
return new Imap({
user: options.user,
password: options.password,
host: options.host,
port: options.port,
tls: options.tls,
connTimeout: options.connTimeout ? options.connTimeout : 10000,
authTimeout: options.authTimeout ? options.authTimeout : 5000,
keepalive: false,
debug: typeof (options.debug) === 'function' ? options.debug : function(debugMsg) {
if(options.debug) console.log(debugMsg);
}
});
},
/**
* Test IMAP connection
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.IMAP.connect} to establish the connection with host
* @return {Promise} Resolved on success and rejected on failure
*/
test: (options) => {
return new Promise((resolve, reject)=>{
const imap = this.IMAP.connect(options.connection);
imap.once('error', err=>reject(err));
imap.once('end', ()=>resolve());
imap.once('ready', ()=>imap.end());
imap.connect();
});
},
/**
* Get whole folder structure for provided IMAP account. Format of the structure is defined by
* the flags described below - If no flag is set returned format is a string array containing
* folder identifiers.
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.IMAP.connect} to establish the connection with host
* @param {boolean} options.jstree - If true returned structure is compatible with a [jsTree]{@link https://github.com/vakata/jstree} module
* @param {boolean} options.raw - If true returned as received from [node-imap]{@link https://github.com/mscdex/node-imap}
* @return {Promise} Resolved with structure defined by the flags above
*/
getFolders: (options) => {
return new Promise((resolve, reject)=>{
var folders;
const imap = this.IMAP.connect(options.connection);
imap.once('error', err=>{
imap.end();
reject(err);
});
imap.once('ready', () => {
imap.getBoxes((err, boxes)=>{
if(err) {
reject(err);
} else {
if(options.jstree) {
folders = [];
function _boxesToJstree(name, obj, parents){
var id = parents ? parents + (obj.delimiter ? obj.delimiter : '.') + name : name;
var jsFolder = {
id: options.connection+'_folder_'+id,
text: name,
type: 'EmailFolder',
data: {
name:id,
attribs:obj.attribs,
delimiter:obj.delimiter
},
children: []
};
if(obj.children && obj.children != null) {
for(var child in obj.children) {
jsFolder.children.push(_boxesToJstree(child, obj.children[child], id));
}
}
return jsFolder;
}
for(var box in boxes) {
folders.push(_boxesToJstree(box, boxes[box]));
}
} else {
if(options.raw) {
folders = boxes;
} else {
folders = [];
function _appendToArray(name, obj, parents) {
var code = parents ? parents + (obj.delimiter ? obj.delimiter : '.') + name : name;
folders.push(code);
if(obj.children && obj.children != null) {
for(var child in obj.children) {
_appendToArray(child, obj.children[child], code);
}
}
}
for(var box in boxes) {
_appendToArray(box, boxes[box]);
}
}
}
}
imap.once('end', ()=>{
resolve(folders);
});
imap.end();
});
});
if(imap.state === 'disconnected') imap.connect();
});
},
/**
* Get folder information for provided IMAP account
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.IMAP.connect} to establish the connection with host
* @param {Object} options.folderName Name of the folder
* @return {Promise} Resolved with the folder IMAP information
*/
getFolderInfo: (options) => {
return new Promise((resolve, reject)=>{
this.IMAP._imapConnectAndOpenBox(options, (info, next)=>{
next(null, info.box);
}, (err, data) => {
err ? reject(err) : resolve(data);
})
});
},
/**
* Get headers of latest messages of specified folder for provided account. Number of messages
* is defined by the length flag.
*
* Seqno is used to sort emails not UID.
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.IMAP.connect} to establish the connection with host
* @param {string} options.folderName - Valid folder name for given IMAP account
* @param {int} options.length - Number of messages returned
* @return {Promise} Resolved with dictionary where Sequence number is used as key and header object as value
*/
getLastMessagesHeaders: (options) => {
return new Promise((resolve, reject)=>{
this.IMAP._imapConnectAndOpenBox(options, (info, next)=>{
if(info.box.messages.total == 0) {
next(null, []);
} else {
var fetch = info.imap.seq.fetch(
Math.max(1, info.box.messages.total-(options.length ? options.length : 50)) + ':*',
{bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)'}
);
this.IMAP._imapFetchHeadersHandler(fetch).then(messages=>{
next(null, messages);
}).catch(next);
}
}, (err, data)=> {
err ? reject(err) : resolve(data);
});
});
},
/**
* Search FROM and TO
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link #module_emailtools.IMAP.connect} to establish the connection with host
* @param {string} options.folderName Valid folder name for given IMAP account
* @param {string} options.search Phrase to search for
* @return {Promise} Resolved with dictionary where Sequence number is used as key and header object as value
*/
searchMessageHeaders: options=>{
return new Promise((resolve, reject)=>{
this.IMAP._imapConnectAndOpenBox(options, (info,next)=>{
if(info.box.messages.total == 0) {
next(null, []);
} else {
info.imap.search([['OR', ['TO', options.search], ['FROM', options.search]]], (err, results) => {
if (err) {
next(err);
} else {
if(results.length > 0) {
const fetch = info.imap.fetch(results, {bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)'});
this.IMAP._imapFetchHeadersHandler(fetch).then(messages=>{
next(null, messages)
}).catch(next);
} else {
next(null, []);
}
}
});
}
}, (err, data)=> {
err ? reject(err) : resolve(data);
});
});
},
/**
* Read and parse message specified by folder name and sequence number.
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link module_emailtools.IMAP.connect} to establish the connection with host
* @param {string} options.folderName Valid folder name for given IMAP account
* @param {int} options.messageSeqNo Message sequence number with the folder
* @return {Promise} Resolved with [MailParser]{@link https://github.com/andris9/mailparser} message object
*/
readMessage: (options) => {
return new Promise((resolve, reject)=>{
this.IMAP._imapConnectAndOpenBox(options, (info, next)=>{
var message = '';
info.imap.seq.fetch(options.messageSeqNo, {bodies: ''}).once('error', fetchErr=>{
next(fetchErr);
}).once('end', ()=>{
_parseMessage(message).then(parsedMessage=>{
next(null, parsedMessage)
}).catch(next);
}).on('message', (msg, seqno)=>{
msg.on('body', (stream, info)=>{
stream.on('data', chunk=>message+=chunk.toString('utf8'));
});
})
}, (err, data)=> {
err ? reject(err) : resolve(data);
});
});
},
/**
* Read and parse messages specified by folder name and sequence number range
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link module_emailtools.IMAP.connect} to establish the connection with host
* @param {string} options.folderName Valid folder name for given IMAP account
* @param {int[]} options.seqNoRange Sequence number range FROM, TO
* @return {Promise} Resolved with [MailParser]{@link https://github.com/andris9/mailparser} message object
*/
readMessages: (options) => {
return new Promise((resolve, reject)=>{
this.IMAP._imapConnectAndOpenBox(options, (info, next)=>{
var msgBuffers = [];
const f = info.imap.seq.fetch(options.seqNoRange[0]+':'+options.seqNoRange[1], {bodies: ''});
f.once('error', next);
f.once('end', ()=>{
var parsers = [];
msgBuffers.forEach(buf=>{
parsers.push(_parseMessage(buf));
});
Promise.all(parsers).then(res=>{
next(null, res);
}).catch(reject);
});
f.on('message', (msg, seqno)=>{
msg.on('body', (stream, info)=>{
var buffer = '';
stream.on('data', chunk=>{
buffer += chunk.toString('utf8');
}).once('end', ()=>{
msgBuffers.push(buffer);
});
});
});
}, (err, data)=> {
err ? reject(err) : resolve(data);
});
});
},
/**
* (NOT IMPLEMENTED) Appends message to the given folder of given account
*
* @param {Object} options
* @param {Object} options.connection Input options passed to [connect()]{@link module_emailtools.IMAP.connect} to establish the connection with host
* @param {string} options.folderName Valid folder name for the given IMAP account
* @param {Object} options.email Email object which shall be parsed and appended
* @return {Promise}
*/
appendMessage: (options) => {
return new Promise((resolve, reject)=>{
resolve();
//if(!options.email) reject(new Error("Email object must be provided"));
//var from = 'From: ';
//var to = 'To: ';
//var subject = 'Subject: ';
//
//if(options.email.from) {
// if(Array.isArray(options.email.from)) {
// options.email.from.forEach(f=>{
// if(typeof(f)==="string") {
// from += '<'+f+'>'
// } else if(typeof(f) === "object") {
// var fStr = '';
// if(f.hasOwnProperty('name')) {
// fStr +=
// } else if(f.hasOwnProperty('address')) {
//
// }
// }
// })
// }
//}
//
//this.IMAP._imapConnectAndOpenBox(options, (info, next)=>{
// info.imap.append('test', {
// flags: 'Seen'
// }, next)
//}, err => {
// err ? reject(err) : resolve();
//});
});
}
};