node-imap模块获取gmail列表文件夹(标签) [英] node-imap module fetch gmail list folders(labels)

查看:131
本文介绍了node-imap模块获取gmail列表文件夹(标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Gmail文件夹列表(labels actually). 我正在使用node js和以下模块: https://github.com/mscdex/node-imap

I'm trying to fetch Gmail folders list(labels actually). I'm using node js and this module : https://github.com/mscdex/node-imap

我想获取所有文件夹和子文件夹. 作者留下的文档不是很清楚.

i want to fetch all folders and sub folders. the documentation that the author left is not so bright.

对此有任何想法吗?

推荐答案

最后,在我努力工作之后,我找到了答案, 这就是我不仅为Google甚至为使用imap标准的每个电子邮件系统都获取文件夹的方式

finally after hard working i found the answer, this is how i get folders not only for google even for every email system that use imap standards

连接到imap服务器后 通过此功能获取所有文件夹.

after connection to imap server the get all folders by this function.

function getFolders(username, callback) {

var folders = [];
if (Connection[username]) {

    Connection[username].once('ready', function() {

        Connection[username].getBoxes(function (err, boxes) {


            if (err) {

                // TODO : parse some error here

            } else {

                folders = imapNestedFolders(boxes);

            }

            return callback(err, folders);
        });
    });

} else {

    return framework.httpError(500, self);
}
}

通过此功能将文件夹解析为漂亮的嵌套树json对象

the parse the folder to a pretty nested tree json object by this function

function imapNestedFolders(folders) {

var FOLDERS = [];
var folder  = {};

for (var key in folders) {

    if (folders[key].attribs.indexOf('\\HasChildren') > -1) {

        var children = imapNestedFolders(folders[key].children);

        folder = {
            name        : key,
            children    : children
        };

    } else {

        folder = {
            name        : key,
            children    : null
        };
    }

    FOLDERS.push(folder);

}
return FOLDERS;
}

您还可以将连接变量更改为所需的变量. 这些函数可用于多个连接,因为连接变量是一个数组,您可以在此处了解更多信息, 我在这里写了如何在节点imap中使用多重连接

you might also change the connection variable to what you wanted. these functions work with multiple connection because of this the connection variable is an array you can read more about this here, i wrote how to use multiple connection in node imap here

如何处理多个在node.js中进行imap连接?

这篇关于node-imap模块获取gmail列表文件夹(标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆