-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.js
115 lines (106 loc) · 3.51 KB
/
email.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
const Imap = require("imap")
let lastMsg = ""
module.exports.getHeader = function (callback) {
let imap = new Imap({
user: process.env.imap_username,
password: process.env.imap_password,
host: process.env.imap_server,
port: process.env.imap_port,
tls: true
});
function openInbox(cb) {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', function () {
setInterval(() => {
openInbox(function (err, box) {
if (err) throw err;
var f = imap.seq.fetch(box.messages.total + ':*', {
bodies: '',
struct: true
});
f.on('message', function (msg, seqno) {
msg.on('body', function (stream, info) {
var buffer = '';
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function () {
let header = Imap.parseHeader(buffer)
if (header["message-id"][0] === lastMsg) {
callback("SEEN");
} else {
lastMsg = header["message-id"][0]
callback(header);
}
});
});
msg.once('attributes', function (attrs) {
});
msg.once('end', function () {
});
});
f.once('error', function (err) {
console.log('Fetch error: ' + err);
});
f.once('end', function () {
//imap.end();
});
});
}, 10000)
});
imap.once('error', function (err) {
console.log(err);
});
imap.once('end', function () {
});
imap.connect();
}
module.exports.getEmail = function (callback) {
let imap = new Imap({
user: process.env.imap_username,
password: process.env.imap_password,
host: process.env.imap_server,
port: process.env.imap_port,
tls: true
});
function openInbox(cb) {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', function () {
openInbox(function (err, box) {
if (err) throw err;
var f = imap.seq.fetch(box.messages.total + ':*', {
bodies: ['TEXT'],
struct: true
});
f.on('message', function (msg, seqno) {
msg.on('body', function (stream, info) {
var buffer = '';
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function () {
callback(buffer)
});
});
msg.once('attributes', function (attrs) {
});
msg.once('end', function () {
});
});
f.once('error', function (err) {
console.log('Fetch error: ' + err);
});
f.once('end', function () {
imap.end();
});
});
});
imap.once('error', function (err) {
console.log(err);
});
imap.once('end', function () {
});
imap.connect();
}