Skip to content

Instantly share code, notes, and snippets.

@outbounder
Last active December 30, 2015 01:49
Show Gist options
  • Save outbounder/7759049 to your computer and use it in GitHub Desktop.
Save outbounder/7759049 to your computer and use it in GitHub Desktop.
fetch email without attachment via imap
var bodies
var attributes_fetch = imap.fetch(emailId, {
struct: true
});
attributes_fetch.on('message', function(msg, seqno) {
msg.once('attributes', function(attrs) {
// just extracts ["HEADER", "1", "1.MIME"] from a message
// which has "HEADER" and "1", "2" parts, where "2" is attachment
bodies = constructBodies(attrs)
});
});
attributes_fetch.once('end', function(){
var email_fetch = imap.fetch(emailId, {
bodies: bodies
});
email_fetch.on("message", function(msg){
var mailparser = new MailParser();
mailparser.on("end", function(mail_object){
// mail_object is missing "text" field here
});
msg.on('body', function(stream, info) {
stream.on('data', function(chunk) {
mailparser.write(chunk)
});
// stream.pipe(mailparser) doesn't work
// for every requested part there is one body emitted,
// which on its stream.end will trigger mailparser
});
msg.once("end", function(){
mailparser.end();
})
})
email_fetch.once("end", function(){
console.log("email fetch done")
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment