我使用node-imap npm模块来获取电子邮件,除了msg的内容之外,我得到了几乎所有的东西。我在他们的git上使用了一个已经提供的示例,如下所示
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) //HEre i am able to view the body
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();
});
});在本例中,我可以通过控制台输出msg的内容,但无法将其作为json返回。
发布于 2014-07-24 15:24:17
返回值为buffer。如果需要字符串,可以在buffer上调用toString()方法。你说你想要JSON的。如果电子邮件正文采用正确的JSON格式,您还可以对其进行解码:
var json = JSON.parse(buffer.ToString());https://stackoverflow.com/questions/24925349
复制相似问题