Guest User

Untitled

a guest
Nov 12th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. getMails: function (req, res) {
  2. var body = req.body
  3. , parser = new MailParser();
  4. if(body.encryptedSecret !== undefined) {
  5. User.findOne({"email": req.user.email}, function (err, user) {
  6. var mailAccount = user.mailAccounts[0]
  7. , imap = new Imap({
  8. user: mailAccount.imap_user,
  9. password: mailAccount.imap_pass,
  10. host: mailAccount.imap_host,
  11. port: mailAccount.imap_port,
  12. tls: true
  13. });
  14. getMailsFromServer(parser, imap, res)
  15. });
  16. } else {
  17. response.error(res, '400', '2-7')
  18. }
  19. }
  20.  
  21. {
  22. "message": "error",
  23. "internalErrrorCode": "2-9",
  24. "data": {
  25. "type": "bad",
  26. "source": "protocol"
  27. }
  28. }
  29.  
  30. imap.once('ready', function () {
  31. imap.openBox('INBOX', true, function (err, box) {
  32. if (err) throw err;
  33. imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2017'] ], function(err, results) {
  34. var f = imap.seq.fetch(results, { bodies: '' });
  35. f.on('message', function (msg, seqno) {
  36. msg.on('body', function (stream, info) {
  37. stream.on('data', function (chunk) {
  38. parser.write(chunk.toString("utf8"));
  39. })
  40. })
  41. msg.once('end', function () {
  42. parser.end();
  43. });
  44. })
  45. f.once('error', function (err) {
  46. error = true
  47. response.error(res, 500, '2-9', err)
  48.  
  49. });
  50. f.once('end', function () {
  51. if (!error) {
  52. parser.on('data', function (data) {
  53. response.success(res, data.html)
  54. });
  55. console.log('Done fetching all messages!');
  56. imap.end();
  57. }
  58. });
  59. })
  60. })
  61. })
Add Comment
Please, Sign In to add comment