function die(err) {
console.log('Uh oh: ' + err);
process.exit(1);
}
var box, cmds, next = 0, cb = function(err) {
if (err)
die(err);
else if (next < cmds.length)
cmds[next++].apply(this, Array.prototype.slice.call(arguments).slice(1));
};
cmds = [
function() { imap.connect(cb); },
function() { imap.openBox('INBOX', false, cb); },
function(result) { box = result; imap.search([ 'UNSEEN', ['SINCE', 'April 5, 2011'] ], cb); },
function(results) {
var msgCache = {},
fetch = imap.fetch(results, { request: { headers: ['from', 'to', 'subject', 'date'] } });
console.log('Now fetching headers!');
fetch.on('message', function(msg) {
msg.on('end', function() {
msgCache[msg.id] = { headers: msg.headers };
console.log(msg.headers.date[0]);
console.log(msg.headers.to[0]);
console.log(msg.headers.from[0]);
console.log(msg.headers.subject[0]);
var from = /(.*)?<(.*?)>/.exec(msg.headers.from[0]);
console.log(from[1]); // nome from
console.log(from[2]); // from
});
});
fetch.on('end', function() {
console.log('Done fetching headers!');
console.log('Now fetching bodies!');
fetch = imap.fetch(results, { request: { headers: false, body: '1' } });
fetch.on('message', function(msg) {
msg.data = '';
msg.on('data', function(chunk) {
msg.data += chunk;
});
msg.on('end', function() {
msgCache[msg.id].body = msg.data;
console.log(msg.data);
});
});
fetch.on('end', function() {
console.log('Done fetching bodies!');
cb(undefined, msgCache);
});
});
},
function(msgs) {
// Do something here with msgs, which contains the headers and
// body (parts) of all the messages you fetched
// console.log(msgs);
//imap.logout(cb);
imap.on('mail', function () {
// body...
console.log("New Email Has Arrived!");
next = 0;
cb();
})
}
];
cb();
当新电子邮件到达imap.on('mail', function ()
时,我希望它再次运行cb()函数。但是,它在console.log之后没有做任何事情。
我做错了什么?
由于
答案 0 :(得分:1)
重置您的next
计数器,您的imap.on('mail', ...
应该在cmds之外,这样它就不会再被绑定,而且会再次被绑定......
答案 1 :(得分:0)