Skip to content

Instantly share code, notes, and snippets.

@lperrin
Created March 7, 2013 11:16
Show Gist options
  • Save lperrin/5107373 to your computer and use it in GitHub Desktop.
Save lperrin/5107373 to your computer and use it in GitHub Desktop.
function fetchEmails = function (range, options, query, mailbox, done) {
var emails = [];
if(!mailbox)
return done({error: 'mailbox not ready'});
query.cb = function (fetch) {
fetch.on('message', function (msg) {
var headers = {},
body = '';
msg.on('headers', function (hdrs) {
headers = hdrs;
});
msg.on('data', function (chunk) {
body += chunk.toString('utf8');
});
msg.on('end', function () {
emails.push({
seq: msg.seqno,
gmid: msg['x-gm-msgid'],
thrid: msg['x-gm-thrid'],
headers: headers,
structure: msg.structure,
body: body
});
});
};
imap.seq.fetch(range, options, query, function (err) {
if(err)
return done(err);
done(null, emails);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment