2014-01-20 77 views

回答

3

該消息的主體處於其中i已CONSOLE.LOG( 「緩衝器」,緩衝液)

我使用節點-IMAP NPM模塊

imap.once('ready', function() { 
    openInbox(function(err, box) { 
     if (err) throw err; 
     var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT'] }); 
     f.on('message', function(msg, seqno) { 
     console.log('Message #%d', seqno); 
     var prefix = '(#' + seqno + ') '; 
     msg.on('body', function(stream, info) { 
      if (info.which === 'TEXT') 
      console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size); 
      var buffer = '', count = 0; 
      stream.on('data', function(chunk) { 
      count += chunk.length; 
      buffer += chunk.toString('utf8'); 

      console.log("BUFFER", buffer) 


      if (info.which === 'TEXT') 
       console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size); 
      }); 
      stream.once('end', function() { 
      if (info.which !== 'TEXT') 
       console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer))); 
      else 
       console.log(prefix + 'Body [%s] Finished', inspect(info.which)); 
      }); 
     }); 
     msg.once('attributes', function(attrs) { 
      console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8)); 
     }); 
     msg.once('end', function() { 
      console.log(prefix + 'Finished'); 
     }); 
     }); 
     f.once('error', function(err) { 
     console.log('Fetch error: ' + err); 
     }); 
     f.once('end', function() { 
     console.log('Done fetching all messages!'); 
     imap.end(); 
     }); 
    }); 
}); 
當場
-1

嘗試:

msg.once('end', function() { 
    console.log(buffer); 
}); 
2

您可以使用此代碼來獲取郵件正文和標記我如圖

var Imap = require("imap"); 
var MailParser = require("mailparser").MailParser; 
var Promise = require("bluebird"); 
Promise.longStackTraces(); 

var imapConfig = { 
    user: 'USERNAME', 
    password: 'PASSWORD', 
    host: 'HOST', 
    port: 993, 
    tls: true 
}; 

var imap = new Imap(imapConfig); 
Promise.promisifyAll(imap); 

imap.once("ready", execute); 
imap.once("error", function(err) { 
    log.error("Connection error: " + err.stack); 
}); 

imap.connect(); 

function execute() { 
    imap.openBox("INBOX", false, function(err, mailBox) { 
     if (err) { 
      console.error(err); 
      return; 
     } 
     imap.search(["UNSEEN"], function(err, results) { 
      if(!results || !results.length){console.log("No unread mails");imap.end();return;} 
      /* mark as seen 
      imap.setFlags(results, ['\\Seen'], function(err) { 
       if (!err) { 
        console.log("marked as read"); 
       } else { 
        console.log(JSON.stringify(err, null, 2)); 
       } 
      });*/ 

      var f = imap.fetch(results, { bodies: "" }); 
      f.on("message", processMessage); 
      f.once("error", function(err) { 
       return Promise.reject(err); 
      }); 
      f.once("end", function() { 
       console.log("Done fetching all unseen messages."); 
       imap.end(); 
      }); 
     }); 
    }); 
} 


function processMessage(msg, seqno) { 
    console.log("Processing msg #" + seqno); 
    // console.log(msg); 

    var parser = new MailParser(); 
    parser.on("headers", function(headers) { 
     console.log("Header: " + JSON.stringify(headers)); 
    }); 

    parser.on('data', data => { 
     if (data.type === 'text') { 
      console.log(seqno); 
      console.log(data.text); /* data.html*/ 
     } 

     // if (data.type === 'attachment') { 
     //  console.log(data.filename); 
     //  data.content.pipe(process.stdout); 
     //  // data.content.on('end',() => data.release()); 
     // } 
    }); 

    msg.on("body", function(stream) { 
     stream.on("data", function(chunk) { 
      parser.write(chunk.toString("utf8")); 
     }); 
    }); 
    msg.once("end", function() { 
     // console.log("Finished msg #" + seqno); 
     parser.end(); 
    }); 
} 

希望這個代碼將幫助你:)

+0

歡迎SO噸。請提供一些背景給你的答案。請參閱http://stackoverflow.com/help/how-to-answer –

相關問題