-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
88 lines (76 loc) · 2.57 KB
/
server.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
const pop3client = require("mailpop3");
const { simpleParser } = require('mailparser');
const HOST = 'your-server-host';
const PORT = 995;
const USERNAME = 'your-email-address';
const PASSWORD = 'your-password';
const client = new pop3client(PORT, HOST, {
tlserrs: false,
enabletls: true,
debug: false,
rejectUnauthorized: false
});
client.on('error', function (err) {
if (err.errno === 111) console.log('Unable to connect to server');
else console.log('Server error occurred');
console.log(err);
});
client.on('connect', function() {
console.log('CONNECT success');
client.login(USERNAME, PASSWORD);
});
client.on('invalid-state', function(cmd) {
console.log('Invalid state. You tried calling ' + cmd);
});
client.on('locked', function(cmd) {
console.log('Current command has not finished yet. You tried calling ' + cmd);
});
client.on('login', function(status, rawdata) {
if (status) {
console.log('LOGIN/PASS success');
client.list();
} else {
console.log('LOGIN/PASS failed');
client.quit();
}
});
client.on('list', function(status, msgcount, msgnumber, data, rawdata) {
if (status === false) {
console.log('LIST failed');
client.quit();
} else {
console.log('LIST success with ' + msgcount + ' element(s)');
if (msgcount > 0) client.retr(msgcount);
else client.quit();
}
});
client.on('retr',async function(status, msgnumber, data, rawdata) {
if (status === true) {
console.log('RETR success for msgnumber ' + msgnumber);
// client.dele(msgnumber);
const parsedEmail = await simpleParser(data);
console.log('Subject:', parsedEmail.subject);
console.log('From:', parsedEmail.from.value.map((from) => from.address).join(', '));
// console.log('To:', parsedEmail.to.value.map((to) => to.address).join(', '));
// console.log('Date:', parsedEmail.date);
console.log('Text body:', parsedEmail.text);
// console.log('HTML body:', parsedEmail.html);
client.quit();
} else {
console.log('RETR failed for msgnumber ' + msgnumber);
client.quit();
}
});
client.on('dele', function(status, msgnumber, data, rawdata) {
if (status === true) {
console.log('DELE success for msgnumber ' + msgnumber);
client.quit();
} else {
console.log('DELE failed for msgnumber ' + msgnumber);
client.quit();
}
});
client.on('quit', function(status, rawdata) {
if (status === true) console.log('QUIT success');
else console.log('QUIT failed');
});