-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailProcessor.js
More file actions
64 lines (56 loc) · 2.36 KB
/
mailProcessor.js
File metadata and controls
64 lines (56 loc) · 2.36 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
var notifier = require('mail-notifier')
, MongoClient = require('mongodb').MongoClient
, fs = require('fs')
, stream = require('stream')
, db = require('./DAL/mongoDao');
var filename = __dirname + "/config.json";
var config = JSON.parse (fs.readFileSync(filename,'utf8'));
var imap = {
user: config.username,
password: config.password,
host: config.imap.host,
port: config.imap.port,
tls: true,// use secure connection
tlsOptions: { rejectUnauthorized: false }
};
//---------------------------------------------------------------------------------------------------------
// Mail Processor mail receive event
//---------------------------------------------------------------------------------------------------------
var mailProcessor = notifier(imap).on('mail', function (mail) {
//Build a custom json email message from "mail" object.
//mail has lot of other properties, please use it as per your requirement.
var msg = {};
msg.date = mail.date;
msg.plaintextbody = mail.text;
msg.subject = mail.subject;
msg.to = JSON.stringify(mail.to);
msg.id = mail.messageId;
msg.from = JSON.stringify(mail.from);
//[Optional]Save the email message to Mongo
db.save(JSON.stringify(msg));
//Check for attachments, if exists extract each attachment and save it to the file system ["uploads" folder]
if (mail.attachments) {
mail.attachments.forEach(function (attachment) {
fs.writeFile( __dirname + "/uploads/" + attachment.generatedFileName, attachment.content, 'base64', function(err) {
if (err!=null)
console.log("Error = " + err);
});
});
}
//[Optional]Save the email message to file system as .txt/.json
/*fs.writeFile("/tmp/test/email_" + mail.messageId + '.txt', JSON.stringify(msg), function (err) {
if (err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});*/
});
//---------------------------------------------------------------------------------------------------------
// Mail Processor end event
//---------------------------------------------------------------------------------------------------------
mailProcessor.on('end',function(){
console.log('Mail Processor ended');
});
//Star the Mail Processor
mailProcessor.start();