return the IMAP results

200 Views Asked by At

I have a getMail method like this:

 async getMail() {
    let email_array = [];
    var buffer = '';
    var imap = new Imap({
        user: 'myuser',
        password: 'mypassword',
        host: 'myhodt',
        port: 143,
        tls: false,
        authTimeout: 3000

    });

    function openInbox(cb) {
        imap.openBox('INBOX', true, cb);
    }
    imap.once('ready', function () {
        openInbox(function (err, box) {
            if (err) throw err;
            var f = imap.seq.fetch('1:1', {
                bodies: '1',
                struct: true
            });
            f.on('message', function (msg, seqno) {
                console.log('Message #%d', seqno);
                msg.on('body', function (stream, info) {
                    stream.on('data', function (chunk) {
                        buffer = chunk.toString('utf8');
                        email_array.push(buffer);
                        console.log("buffer", buffer)
                    });
                });
                msg.once('end', function () {
                    console.log('Finished');
                });
            });
        });
    });
    imap.once('error', function (err) {
        console.log(err);
    });
    imap.connect();
    return new Promise((resolve, reject) => {
        imap.once('end', async function () {
            resolve(email_array);
        });
    })
}

when I call this method, the value of the email prints in the console but It does not return in postman.

I read this post but I can not solve this issue

0

There are 0 best solutions below