-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (100 loc) · 3.7 KB
/
server.js
File metadata and controls
129 lines (100 loc) · 3.7 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
const express = require('express');
const app = express();
const MailListener = require('mail-listener2');
const bodyParser = require('body-parser');
const jsonParses = bodyParser.json();
const PORT = process.env.PORT || 3000;
const COURSES = [
{ id: 1, name: 'NodeJS' },
{ id: 2, name: 'PHP' },
{ id: 3, name: 'ReactJS' },
]
app.get('/', (req, res) => {
res.send('Hello world!');
});
app.post('/api/courses2', (req, res) => {
res.send(COURSES);
});
app.get('/api/courses', (req, res) => {
res.send(COURSES);
});
app.post('/mail', jsonParses, (req, res, next) => {
let username = req.body.username || 'test';
let password = req.body.password || 'test123';
let host = req.body.host || 'mail.bravo.com.vn';
let port = req.body.port || 143;
let mailbox = req.body.mailbox || 'INBOX';
let date = req.body.date || curr_date;
let getBody = req.body.getBody || 'true';
var mailListener = new MailListener({
username: username,
password: password,
host: host,
port: port,
tls: false,
connTimeout: 10000, // Default by node-imap
authTimeout: 5000, // Default by node-imap,
debug: null, // Or your custom function with only one incoming argument. Default: null
tlsOptions: { rejectUnauthorized: false },
mailbox: mailbox, // mailbox to monitor
searchFilter: [["ON", date]], // the search filter being used after an IDLE notification has been retrieved
markSeen: false, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: {streamAttachments: false}, // options to be passed to mailParser lib.
attachments: false, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start(); // start listening
mailListener.on('server:connected', () =>{
console.log('imapConnected');
});
mailListener.on('server:disconnected', () =>{
console.log('imapDisconnected');
});
mailListener.on('error', () => {
mailListener.stop();
res.writeHead(404);
res.send(JSON.stringify({
success: false,
data: null
}));
})
let lst = [];
mailListener.on('mail', (mail2, seqno, attributes) => {
let message = {
from: undefined,
to: undefined,
subject: undefined,
date: undefined,
body: undefined,
messageId: undefined
};
if (typeof mail2.to !== 'undefined')
message.to = mail2.to[0].address;
if (typeof mail2.from !== 'undefined')
message.from = mail2.from[0].address;
message.from = mail2.from[0].address;
message.subject = mail2.subject;
message.date = mail2.receivedDate;
message.messageId = mail2.messageId;
if (typeof mail2.cc !== 'undefined'){
message.cc = '';
mail2.cc.forEach(ccDetail =>{
message.cc += ';' + ccDetail.address;
})
}
if (getBody == 'true'){
message.body = mail2.text;
}
lst.push(message);
if (lst.length == mail2.total) {
mailListener.stop();
res.setHeader(`Content-Type`, `application/json`);
res.send(JSON.stringify({
success: true,
data: lst
}));
}
})
});
app.listen(PORT, () => { console.log(`Listening port ${PORT}`) });