Skip to content

Instantly share code, notes, and snippets.

@AdamMagaluk
Created February 25, 2013 16:00
Show Gist options
  • Save AdamMagaluk/5030815 to your computer and use it in GitHub Desktop.
Save AdamMagaluk/5030815 to your computer and use it in GitHub Desktop.
Node Imap
var Imap = require('imap'),
inspect = require('util').inspect;
var imap = new Imap({
user: '[email protected]',
password: 'XXXXX',
host: 'imap.gmail.com',
port: 993,
secure: true
});
function show(obj) {
return inspect(obj, false, Infinity);
}
function die(err) {
console.log('Uh oh: ' + err);
process.exit(1);
}
function openInbox(cb) {
imap.connect(function(err) {
if (err) die(err);
imap.openBox('INBOX', true, cb);
});
}
setInterval(function(){
console.log("Getting Messages");
openInbox(function(err, mailbox) {
if (err){
die(err);
}
imap.search([ 'UNSEEN',['FROM','[email protected]'] ], function(err, results) {
if (err || results.length ==0){
console.log(err);
}else{
imap.fetch(results,
{ headers: ['from', 'to', 'subject', 'date'],
cb: function(fetch) {
fetch.on('message', function(msg) {
console.log('Saw message no. ' + msg.seqno);
msg.on('headers', function(hdrs) {
console.log('Headers for no. ' + msg.seqno + ': ' + show(hdrs));
});
msg.on('end', function() {
console.log('Finished message no. ' + msg.seqno);
});
});
}
}, function(err) {
if (err) console.log(err);
console.log('Done fetching all messages!');
}
);
}
});
});
},5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment