2

gmail のメールを取得し、そのデータを CouchBase に保存する単純な nodejs スクリプトを作成しています。

問題は、最後のものを除くすべてのヘッダー フィールドが未定義として表示されることです。

コード:

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!

メッセージの 1 つは次のようになります。

{ 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',...}

したがって、ヘッダーは誤ってテキストとして解釈されますか?

4

0 に答える 0