forked from QuarkGluonPlasma/nestjs-course-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.js
65 lines (55 loc) · 1.92 KB
/
index2.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
const { MailParser } =require('mailparser');
const fs = require('fs');
const path = require('path');
const Imap = require('imap');
var imap = new Imap({
user: '[email protected]',
password: '你的授权码',
host: 'imap.qq.com',
port: 993,
tls: true
});
imap.once('ready', () => {
imap.openBox('INBOX', true, (err) => {
imap.search([['SEEN'], ['SINCE', new Date('2023-07-10 19:00:00').toLocaleString()]], (err, results) => {
if (!err) {
handleResults(results);
} else {
throw err;
}
});
});
});
function handleResults(results) {
imap.fetch(results, {
bodies: '',
}).on('message', (msg) => {
const mailparser = new MailParser();
msg.on('body', (stream) => {
const info = {};
stream.pipe(mailparser);
mailparser.on("headers", (headers) => {
info.theme = headers.get('subject');
info.form = headers.get('from').value[0].address;
info.mailName = headers.get('from').value[0].name;
info.to = headers.get('to').value[0].address;
info.datatime = headers.get('date').toLocaleString();
});
mailparser.on("data", (data) => {
if (data.type === 'text') {
info.html = data.html;
info.text = data.text;
const filePath = path.join(__dirname, 'mails', info.theme + '.html');
fs.writeFileSync(filePath, info.html || info.text)
console.log(info);
}
if (data.type === 'attachment') {
const filePath = path.join(__dirname, 'files', data.filename);
const ws = fs.createWriteStream(filePath);
data.content.pipe(ws);
}
});
});
});
}
imap.connect();