如何将RFC822消息正文体存储为String?

时间:2017-12-21 07:48:28

标签: javascript node.js npm inbox

var inbox = require("inbox");

var client = inbox.createConnection(false, "imap.gmail.com", {
    secureConnection: true,
    auth:{
        user: "myemail@gmail.com",
        pass: "mypass"
    }
});

client.connect();

client.on("connect", function(){
    client.openMailbox("INBOX", function(error, info){
        if(error) throw error;

        client.listMessages(-10, function(err, messages){
          var datas = [];
          var data;
            messages.forEach(function(message){
                data = message.UID + ": " + message.title;
                datas.push(data);
            });
            var messageStream = client.createMessageStream(22048);
            console.log(messageStream);
            client.close();
        });

    });
});

上面的代码返回

Stream {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined }

但是通过替换

var messageStream = client.createMessageStream(22048);
console.log(messageStream);

client.createMessageStream(123).pipe(process.stdout, {end: false});

我在终端中获得了整个RFC822消息体 如何将RFC822消息正文体存储为String? 我尽我所能去做,但每次都返回相同 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

由于它是可读的流,因此您可以使用NodeJS - Class: stream.Readable

您可以使用data事件从流和end事件中获取消息,以了解何时没有更多数据

  

事件:'数据'

const readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`);
});
     

事件:'结束'

const readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`);
});
readable.on('end', () => {
  console.log('There will be no more data.');
});

P.S。 createMessageStream接受消息UID作为参数。

在您的情况下,您可能需要使用一些承诺基函数来包装它

示例

var inbox = require("inbox");

var client = inbox.createConnection(false, "imap.gmail.com", {
    secureConnection: true,
    auth: {
        user: "myemail@gmail.com",
        pass: "mypass"
    }
});

client.connect();

client.on("connect", function() {

    client.openMailbox("INBOX", function(error, info) {
        if (error) throw error;

        var emails = [];

        var promises = [];

        new Promise(function(resolve, reject) {

            client.listMessages(-10, function(err, messages) {

                if (err) {
                    reject(err)
                    return;
                };

                messages.forEach(function(message) {

                    var promise = new Promise(function(resolve, reject) {

                        var email = {
                            UID: messages.UID,
                            title: message.title,
                            body: ""
                        }

                        client.createMessageStream(message.UID)
                            .on('data', (chunk) => {
                                email.body += chunk.toString();
                            }).on('end', () => {
                                //return the email object
                                resolve(email);

                            }).on('error', (err) => {
                                reject(err);
                            });
                    })

                    promises.push(promise);

                });

                resolve();

            });

        }).then(() => {

            return Promise.all(promises).then(function(emails) {
                console.log(emails);
                console.log("closing");
                client.close();
            })
        })

    });
});