Reputation: 157
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.
any idea about this?
Upvotes: 2
Views: 3391
Reputation: 157
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
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);
}
}
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;
}
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
How can i handle multiple imap connections in node.js?
Upvotes: 3