1

我正在使用 npm 库imap来获取电子邮件的收件箱,如下所示:

readMails(path, skip, limit){
        return new Promise((resolve, reject)=>{
            let mails = [], total;

            this.imap.once('ready', (err) =>{
                this.imap.openBox(path, false, (err, box)=>{
                        total = box.messages.total;

                        if(total<limit) return this.imap.end();

                        var f = this.imap.seq.fetch(`${skip+1}:${limit}`, {
                            bodies: [''],
                            struct: true
                        });
                        f.on('message', (msg, seqno) => {
                            let buffer = '', mail = { content: null, attrs: null }; 

                            console.log('Message #%d', seqno);
                            var prefix = '(#' + seqno + ') ';
                            msg.on('body', (stream, info) => {
                                //console.log(stream)
                                let chunk = ''
                                stream.on('data',(chunk)=>{
                                    buffer += chunk.toString('utf8')
                                })
                            });
                            msg.once('attributes', (attrs) => {
                                mail.attrs = attrs
                            console.log(prefix + 'Attributes: %s', attrs, false, 8);
                            });
                            msg.once('end', () => {
                                console.log(prefix + 'Finished');
                                simpleParser(buffer, (err, content)=>{
                                    console.log(err, mail);
                                    let {date,from,html,messageId,subject,text,textAsHtml} = content;
                                    mail.content = {date,from,html,messageId,subject,text,textAsHtml};  
                                    mails.push(mail);
                                })
                            });
                        });
                        f.once('error', (err) => {
                            console.log('Fetch error: ' + err);
                        });
                        f.once('end', () => {
                            console.log('Done fetching all messages!');
                            this.imap.end();
                        });
                })
            });
            this.imap.once('error', function(err) {
                reject()
                console.log(err);
            });

            this.imap.once('end', function() {
                resolve({ mails, total });
                console.log('Connection ended');
            });

            this.imap.connect();
        })
    }

这提供了所需的输出,但加载时间太长,因为它也处理其附件。

我想知道是否有任何方法可以跳过附件下载和处理,这会在服务器上节省太多时间。

4

0 回答 0