{"action":"created","issue":{"url":"https://api.github.com/repos/chadxz/imap-simple/issues/12","repository_url":"https://api.github.com/repos/chadxz/imap-simple","labels_url":"https://api.github.com/repos/chadxz/imap-simple/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/chadxz/imap-simple/issues/12/comments","events_url":"https://api.github.com/repos/chadxz/imap-simple/issues/12/events","html_url":"https://github.com/chadxz/imap-simple/issues/12","id":158233442,"number":12,"title":"Problem fetching many mails with their attachments","user":{"login":"hengel2810","id":11571150,"avatar_url":"https://avatars.githubusercontent.com/u/11571150?v=3","gravatar_id":"","url":"https://api.github.com/users/hengel2810","html_url":"https://github.com/hengel2810","followers_url":"https://api.github.com/users/hengel2810/followers","following_url":"https://api.github.com/users/hengel2810/following{/other_user}","gists_url":"https://api.github.com/users/hengel2810/gists{/gist_id}","starred_url":"https://api.github.com/users/hengel2810/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hengel2810/subscriptions","organizations_url":"https://api.github.com/users/hengel2810/orgs","repos_url":"https://api.github.com/users/hengel2810/repos","events_url":"https://api.github.com/users/hengel2810/events{/privacy}","received_events_url":"https://api.github.com/users/hengel2810/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-06-02T20:34:56Z","updated_at":"2016-06-02T21:25:48Z","closed_at":null,"body":"Hello, \r\nI'm having a problem fetching many mails with their attachments. It seems my script is freezing while loading the mails. Is it possible that nodeJS is to slow for loading the attachments for hundreds of mails ? \r\n\r\nThis is the code I'm using to fetch the mails and their attachments:\r\n\r\n\r\n```\r\nimaps.connect(config).then(function (connection) {\r\n\r\n    connection.openBox('INBOX').then(function () {\r\n\r\n        var days = 30;\r\n        var hours = 24 * days;\r\n        var delay = hours * 3600 * 1000;\r\n        var currentDate = new Date();\r\n        currentDate.setTime(Date.now() - delay);\r\n        currentDate = currentDate.toISOString();\r\n        var searchCriteria = ['ALL', ['SINCE', currentDate]];\r\n        var fetchOptions = { bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)'], struct: true };\r\n\r\n        return connection.search(searchCriteria, fetchOptions);\r\n    }).then(function (messages) {\r\n        console.log(\"### Count: \" + messages.length);\r\n        var mails = [];\r\n        var lastMailIDWithAttachments = idOfLastMailWithAttachments(messages);\r\n        messages.forEach(function (message) {\r\n            var subject = \"### NIL ###\";\r\n            var date = \"### NIL ###\";\r\n            var sender = \"### NIL ###\";\r\n            var uid = message.attributes.id;\r\n\r\n\r\n            message.parts.forEach(function(part)\r\n            {\r\n                subject = part.body.subject[0];\r\n                date = part.body.date[0];\r\n                sender = part.body.from[0];\r\n            })\r\n\r\n            var parts = imaps.getParts(message.attributes.struct);\r\n            console.log(uid + \" - \" + parts.length);\r\n\r\n            var attachments = [];\r\n            parts.forEach(function(part)\r\n            {\r\n                if (part.disposition)\r\n                {\r\n                    if (part.disposition.type.toLowerCase() == \"attachment\")\r\n                    {\r\n                        if (part.disposition.params.filename)\r\n                        {\r\n                            console.log(part.disposition.params.filename);\r\n                            connection.getPartData(message, part).then(function (partData) {\r\n                                // var fullFileName = sender + \" ###_### \" + part.disposition.params.filename;\r\n                                // fullFileName = fullFileName.split(\"\\\"\").join(\"\");\r\n                                // fullFileName = fullFileName.split(\"\\'\").join(\"\");\r\n                                // fullFileName = fullFileName.split(\"\\/\").join(\"\");\r\n                                // fullFileName = fullFileName.split(\" \").join(\"_\");\r\n                                var attachment = {\r\n                                    id:part.partID,\r\n                                    filename:part.disposition.params.filename,\r\n                                    sender:sender\r\n                                };\r\n\r\n                                var dir = '../PDF_Bills';\r\n                                if (!fs.existsSync(dir))\r\n                                {\r\n                                    fs.mkdirSync(dir);\r\n                                }\r\n                                fs.writeFile(dir + \"/\" + attachment.filename, partData, function (err) {\r\n                                  if (err) return console.log(err);\r\n                                });\r\n                                attachments.push(attachment);\r\n\r\n                                var partIndex = parts.indexOf(part);\r\n                                if (partIndex === parts.length-1)\r\n                                {\r\n                                    var mailObject = {\r\n                                        subject:subject,\r\n                                        date:date,\r\n                                        did:und,\r\n                                        attachments:attachments\r\n                                    };\r\n                                    mails.push(mailObject);\r\n                                    if (lastMailIDWithAttachments === mailObject.uid)\r\n                                    {\r\n                                        connection.end();\r\n                                        console.log(\"FINISHED\");\r\n                                        var jsonString = JSON.stringify(mails);\r\n                                        fs.writeFile(\"./mails.json\", jsonString, function (err) {\r\n                                          if (err) return console.log(err);\r\n                                        });\r\n                                    }\r\n                                }\r\n                            });\r\n                        }\r\n                    }\r\n                }\r\n            });\r\n        });\r\n    });\r\n\r\n    connection.once('error', function (err) {\r\n        console.log(err);\r\n    });\r\n});\r\n\r\n\r\n```\r\nI hope somebody can help me.\r\nHenrik"},"comment":{"url":"https://api.github.com/repos/chadxz/imap-simple/issues/comments/223427497","html_url":"https://github.com/chadxz/imap-simple/issues/12#issuecomment-223427497","issue_url":"https://api.github.com/repos/chadxz/imap-simple/issues/12","id":223427497,"user":{"login":"chadxz","id":309219,"avatar_url":"https://avatars.githubusercontent.com/u/309219?v=3","gravatar_id":"","url":"https://api.github.com/users/chadxz","html_url":"https://github.com/chadxz","followers_url":"https://api.github.com/users/chadxz/followers","following_url":"https://api.github.com/users/chadxz/following{/other_user}","gists_url":"https://api.github.com/users/chadxz/gists{/gist_id}","starred_url":"https://api.github.com/users/chadxz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chadxz/subscriptions","organizations_url":"https://api.github.com/users/chadxz/orgs","repos_url":"https://api.github.com/users/chadxz/repos","events_url":"https://api.github.com/users/chadxz/events{/privacy}","received_events_url":"https://api.github.com/users/chadxz/received_events","type":"User","site_admin":false},"created_at":"2016-06-02T21:25:48Z","updated_at":"2016-06-02T21:25:48Z","body":"you might wanna try running your code under a debugger if you're having trouble figuring out where things are going into the weeds. [Webstorm](https://www.jetbrains.com/webstorm/) has one built in, or you can just attach something like [Node Inspector](https://github.com/node-inspector/node-inspector). \r\n\r\nThat, or just add more logging! heh"}}
{"actor":{"display_login":"chadxz"}}