Created
October 12, 2011 17:32
-
-
Save troyk/1281918 to your computer and use it in GitHub Desktop.
Get a list of email addresses from your gmail sent folder using node
This file contains 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
// Based off an example from node-imap, will output a list of email addresses from your | |
// gmail sent folder (so you can copy/paste to the BCC field and let people know your | |
// sorry for using the same password all over the internet resulting in your buddies | |
// getting a lame spam message) | |
var ImapConnection = require('imap').ImapConnection, | |
imap = new ImapConnection({ | |
username: '{{replace}}@gmail.com', | |
password: '{{replace}}', | |
host: 'imap.gmail.com', | |
port: 993, | |
secure: true | |
}); | |
function die(err) { | |
console.log('Uh oh: ' + err); | |
process.exit(1); | |
} | |
var addresses = [], 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('[Gmail]/Sent Mail', false, cb); }, | |
function(result) { box = result; imap.search([ 'ALL', ['SENTSINCE', 'October 10, 2011'] ], cb); }, | |
function(results) { | |
var fetch = imap.fetch(results, { request: { headers: ['from', 'to', 'subject', 'date'] } }); | |
fetch.on('message', function(msg) { | |
msg.on('end', function() { | |
addresses.push(msg.headers.to[0]); | |
}); | |
}); | |
fetch.on('end', function() { | |
console.log('addresses: ', addresses.length); | |
console.log(addresses.sort().join(',')); | |
imap.logout(cb); | |
}); | |
} | |
]; | |
cb(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment