-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentdiffemail.js
More file actions
96 lines (88 loc) · 2.53 KB
/
sentdiffemail.js
File metadata and controls
96 lines (88 loc) · 2.53 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
// For getting all sent emails
const express = require("express");
const app = express();
const Imap = require("node-imap");
const { simpleParser } = require("mailparser");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
const Mails = [];
// for sent email
// const startDate = new Date("2023-08-01");
// const endDate = new Date("2023-11-31");
// [
// ["SENTSINCE", startDate.toISOString()],
// ["BEFORE", endDate.toISOString()],
// ],
const imapConfig = {
user: "email",
password: "password",
host: "imap.secureserver.net",
port: 993,
tls: true,
authTimeout: 30000,
connTimeout: 30000,
};
const imap = new Imap(imapConfig);
const getEmails = () => {
try {
imap.once("ready", () => {
// imap.getBoxes((err, boxes) => {
// console.log(boxes);
// imap.end();
// return;
// });
imap.openBox("INBOX", false, () => {
imap.search(["ALL"], (err, results) => {
if (results.length === 0) {
console.log("No mails to fetch");
imap.end();
return;
}
const limit = results.slice(0, 10);
const f = imap.fetch(limit, { bodies: "" });
f.on("message", (msg) => {
msg.on("body", (stream) => {
simpleParser(stream, async (err, parsed) => {
// const {from, subject, textAsHtml, text} = parsed;
console.log(parsed);
Mails.push(parsed);
/* Make API call to save the data
Save the retrieved data into a database.
E.t.c
*/
});
});
// msg.once("attributes", (attrs) => {
// const { uid } = attrs;
// imap.addFlags(uid, ["\\Seen"], () => {
// // Mark the email as read after reading it
// console.log("Marked as read!");
// });
// });
});
f.once("error", (ex) => {
return Promise.reject(ex);
});
f.once("end", () => {
console.log("Done fetching all messages!");
console.log(Mails.length);
imap.end();
});
});
});
});
imap.once("error", (err) => {
console.log(err);
});
imap.once("end", () => {
console.log(Mails.length);
console.log("Connection ended");
});
imap.connect();
} catch (ex) {
console.log("an error occurred");
}
};
getEmails();
app.listen(5000, () => {
console.log("APP RUNNING");
});