-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.js
66 lines (55 loc) · 1.44 KB
/
hello.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
import { simpleParser } from 'mailparser';
import Imap from 'imap';
async function handler() {
console.log(process.env.IMAP_HOST)
const imapConfig = {
user: process.env.IMAP_USER,
password: process.env.IMAP_PASSWORD,
host: process.env.IMAP_HOST,
port: 993,
tls: true,
};
const imap = new Imap(imapConfig);
imap.once('ready', () => {
imap.openBox('INBOX', true, (err, box) => {
if (err) {
return;
}
const fetch = imap.seq.fetch('1:10', {
bodies: '',
struct: true,
});
const emails = [];
fetch.on('message', (msg, seqno) => {
msg.on('body', (stream, info) => {
simpleParser(stream, (err, parsed) => {
if (err) {
console.error('Error parsing email:', err);
return;
}
emails.push({
subject: parsed.subject,
from: parsed.from.text,
date: parsed.date,
text: parsed.text,
});
console.log(emails)
});
});
});
fetch.once('end', () => {
imap.end();
});
});
});
imap.once('error', (err) => {
console.error('IMAP error:', err);
});
imap.connect();
}
export async function GET(request) {
handler()
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec));
await sleep(5000);
return new Response(`Hello2 from ${process.env.VERCEL_REGION}`);
}