Created
April 19, 2013 07:51
-
-
Save kzrl/5418800 to your computer and use it in GitHub Desktop.
Nodemail example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Imap = require('imap'), | |
inspect = require('util').inspect; | |
var imap = new Imap({ | |
user: '', | |
password: '', | |
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); | |
}); | |
} | |
openInbox(function(err, mailbox) { | |
if (err) die(err); | |
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { | |
if (err) die(err); | |
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) throw err; | |
console.log('Done fetching all messages!'); | |
imap.logout(); | |
} | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment