imap + email解析器,除最后一个之外,所有头字段都是未定义的

时间:2014-11-02 15:26:42

标签: node.js imap email-parsing

我正在构建一个简单的nodejs脚本,它将获取我的gmail电子邮件并将其数据存储在CouchBase中。

问题是除了最后一个之外的所有标题字段都显示为未定义。

代码:

var Imap = require('imap'),
    inspect = require('util').inspect,
    MailParser = require("mailparser").MailParser,
    couchbase = require('couchbase'),
    cluster = new couchbase.Cluster(),
    messages = [];

var imap = new Imap({
    user: '***@gmail.com',
    password: '***',
    host: 'imap.gmail.com',
    port: 993,
    tls: true
});

function openInbox(cb) {
    imap.openBox('INBOX', true, cb);
}

function saveToCouchbase(data){
    var bucket = cluster.openBucket('emails');
    data.forEach(function(doc, index){
        bucket.upsert(index + '', doc, function(err, res){
            if (err) throw err;
            console.log(doc.subject);
        })
    });
}

function main() {
// using the functions and variables already defined in the first example ...
    imap.once('ready', function() {
        openInbox(function (err, box) {
            if (err) throw err;
            var f = imap.seq.fetch('1:30', { bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'] });
            f.on('message', function (msg, seqno) {
                var prefix = '(#' + seqno + ') ';
                var parser = new MailParser();
                parser.on('end', function(mailObj){
                    messages.push(mailObj);
                });
                msg.on('body', function (stream, info) {
                    var buffer = '', count = 0;
                    stream.on('data', function (chunk) {
                        parser.write(chunk);
                    });
                });

                f.once('error', function (err) {
                    console.log('Fetch error: ' + err);
                });
                f.once('end', function () {
                    console.log('Done fetching all messages!');
                    imap.end();
                });

                msg.once('attributes', function (attrs) {
                    console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
                });
                msg.on('end', function () {
                    console.log(prefix + 'Finished');
                    parser.end();
                });
            });
        });
    });
    imap.once('error', function(err) {
        console.log(err);
    });

    imap.once('end', function() {
        console.log('Connection ended');
        console.log(messages);
        saveToCouchbase(messages);
    });

    imap.connect();
}

main();

' doc.subject'的控制台日志输出:

undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
Your Co-Worker Invited You to Join!

其中一条消息如下:

{ text: 'Date: Sun, 05 May 2013 12:07:59 +0300\nSubject: Mentor Graphics -  =?UTF-8?B?15TXqdeZ16o=?=\n =?UTF-8?B?15XXoyDXlNeQ15XXmNeV157XmNeZIA==?=\n =?UTF-8?B?15HXnNeZ16DXp9eT15DXmQ==?=\n =?UTF-8?B?158g15PXqNeaINeY15XXnteZ15LXlQ==?=\n =?UTF-8?B?INei15XXnteTINec15TXodeq15nXmQ==?= =?UTF-8?B?150=?=\nFrom: Tomigo <dontreply@atav.co.il>\nTo: Oleg b<hans.landsberg@gmail.com>\n\n',...}

因此标题被误解为文本?。

0 个答案:

没有答案