Why can't I use imap to read email messages multiple times?

I have the following node/typescript process running on Mac.

ip-192-168-1-3:~ reza.razavipour$ uname -a Darwin ip-192-168-1-3.us-west-2.compute.internal 18.7.0 Darwin Kernel Version 18.7.0: Thu Jan 23 06:52:12 PST 2020; root:xnu-4903.278.25~1/RELEASE_X86_64 x86_64 ip-192-168-1-3:~ reza.razavipour$ node -v v12.14.0 ip-192-168-1-3:~ reza.razavipour$ npm -v "imap": "^0.8.19" "typescript": "^3.9.7", 

I have a node server that is supposed to read emails, it is called from a specific route. Nothing fancy.

The following code is in a ts file and it exports one function namely

export const connect = () => {     imap.connect() } 

first time i call this function, all is well, I connect I read the specific email and no issues.

Next time I call the exported function nothing happens. Is there a clean up that i am not doing or what am I missing?

const Imap = require('imap');  const MailParser = require('mailparser').MailParser; import { logger } from '../util/logger';      var imap = new Imap({             user: 'user',             password: 'pw',             host: 'host',             port: 32,             tls: true,             tlsOptions: {rejectUnauthorized: false}         }     );          function openInbox(cb) {         imap.openBox('INBOX', false, cb);     }          function processMessage(msg, seq) {         logger.debug('Processing msg #' + seq);              const parser = new MailParser();         parser.on('data', data => {             if (data.type === 'attachment') {                 console.log(data.filename);                 // data.content.pipe(process.stdout);                 data.content.on('end', () => data.release());             }         });              msg.on('body', stream => {             stream.on('data', function(chunk) {                 parser.write(chunk.toString('utf8'));             });         });         msg.once('end', () => {             logger.debug('Finished msg #' + seq);             parser.end();         });     }          imap.once('ready', function() {         openInbox((err, box) => {             try {                 if (err) {                     logger.error(err);                     imap.end();                     return;                 }                      imap.search(['ALL', ['SUBJECT', '32']], (err, results) => {                     if (err) {                         logger.error(err);                         imap.end();                         return;                     }                          if (!results || !results.length) {                         logger.error('No unread mails');                         imap.end();                         return;                     }                          var f = imap.fetch(results, {bodies: ''});                     results.forEach(result => {                         const f = imap.fetch(result, {bodies: ''});                         f.on('message', processMessage);                         f.once('error', (err) => {                             logger.error('Fetch error: ' + err);                         });                         f.once('end', () => {                             logger.debug('Done fetching all unseen messages.');                             imap.end();                         });                     });                 });             } catch (e) {                 logger.error(e);             }         });     });          imap.once('error', (err) => {         logger.error(err);     });          imap.once('end', () => {         logger.debug('Connection ended');     });          export const connect = () => {         imap.connect()     } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.