如果没有符合我们搜索条件的电子邮件,我正在尝试退出脚本。如果我不做imap.end()
,它永远不会终止。当我添加imap.end时,我得到一个ECONNRESET
。什么是提前退出IMAP的正确方法?
function openInbox(cb) {
imap.openBox('[Gmail]/All Mail', true, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
if (err) throw err;
const aDayAgo = moment().subtract(1,'d');
imap.search([ ['SUBJECT', DPR_SUBJECT], ['SINCE', aDayAgo] ], function(err, results) {
if (err) throw err;
if (results.length == 0){
logging.log("No mail");
db.disconnect();
imap.end();
return;
}
var f = imap.fetch(results, {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true
});
f.on('message', function(msg, seqno) {
logging.verbose('Message #%d', seqno);
msg.once('end', function() {
logging.verbose(prefix + 'Finished');
});
});
f.once('error', function(err) {
logging.verbose('Fetch error: ' + err);
});
f.once('end', function() {
logging.log('Done fetching all messages!');
db.disconnect();
imap.end();
});
});
});
});
imap.once('error', function(err) {
logging.error(err.stack);
});
imap.once('end', function() {
logging.verbose('Connection ended');
});
exports.checkForMail = () => {
return imap.connect();
}