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

How to store the RFC822 message body body as String?

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();
        });

    });
});

以上代码returns

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 消息体 body 存储为 String? 我尽力做到了,但每次 returns 都一样 我该怎么做?

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

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

Event: 'data'

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

Event: 'end'

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 作为参数。

在你的情况下,你可能需要用一些 promise 基础函数来包装它

例子

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();
            })
        })

    });
});