fix: watch email
This commit is contained in:
46
src/index.ts
46
src/index.ts
@@ -1,11 +1,14 @@
|
||||
const chalk = require('chalk');
|
||||
const {ImapFlow} = require('imapflow');
|
||||
const simpleParser = require('mailparser').simpleParser
|
||||
const dotenv = require('dotenv')
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
const POLLING_TIMEOUT = 30
|
||||
|
||||
const client = new ImapFlow({
|
||||
const MARK_STRING = 'test'
|
||||
|
||||
const clientConfig = {
|
||||
host: process.env.EMAIL_IMAP_SERVER,
|
||||
port: 993,
|
||||
secure: true,
|
||||
@@ -13,38 +16,45 @@ const client = new ImapFlow({
|
||||
user: process.env.EMAIL_ADDRESS,
|
||||
pass: process.env.EMAIL_PASSWORD,
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const markString = 'test'
|
||||
}
|
||||
|
||||
const main = async () => {
|
||||
const client = new ImapFlow(clientConfig);
|
||||
await client.connect(); // Wait until client connects and authorizes
|
||||
await client.mailboxOpen('INBOX'); // Open mailbox
|
||||
const lock = await client.getMailboxLock('INBOX'); // Open mailbox
|
||||
|
||||
try {
|
||||
|
||||
let listSeenMsgId = await client.search({seen: false}); // search only unseen messages
|
||||
console.log('📌', 'LIST:', listSeenMsgId)
|
||||
console.log('📌', chalk.cyan('UNSEEN LIST:'), listSeenMsgId)
|
||||
|
||||
for await (let message of client.fetch(listSeenMsgId,
|
||||
{
|
||||
envelope: true,
|
||||
bodyParts: true,
|
||||
bodyStructure: true
|
||||
source: true
|
||||
})) {
|
||||
|
||||
if (message.envelope.subject === MARK_STRING) {
|
||||
let email = Buffer.from(message.source).toString()
|
||||
const parsed = await simpleParser(email)
|
||||
console.log('📌', chalk.cyan('Message:'), chalk.yellow(parsed.text));
|
||||
|
||||
client.messageFlagsAdd(message.seq.toString(), ['\\Seen'])
|
||||
.then((result: any) => console.log('📌', chalk.cyan(`Set seen flag for id-${message.seq}`), result))
|
||||
.catch((error: any) => console.log(error))
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
} finally {
|
||||
lock.release(); // mandatory release mailbox
|
||||
await client.logout(); // log out and close connection
|
||||
console.log('🧡', chalk.cyan('Heartbeat is ok'))
|
||||
}
|
||||
|
||||
await client.logout(); // log out and close connection
|
||||
};
|
||||
|
||||
//main().then().catch(err => console.error(err)); // start once for development
|
||||
|
||||
main().then().catch(err => console.error(err));
|
||||
|
||||
// setInterval(() => {
|
||||
// main().then(() => console.log('🧡', 'Heartbeat is ok')).catch(err => console.error(err));
|
||||
// }, 60 * 1000)
|
||||
setInterval(async () => {
|
||||
main().then().catch(err => console.error(err));
|
||||
}, POLLING_TIMEOUT * 1000)
|
||||
|
||||
Reference in New Issue
Block a user