Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. var fs = require('file-system');
  2. var Imap = require('imap'),
  3. inspect = require('util').inspect;
  4.  
  5. var imap = new Imap({
  6. user: 'Email ID',
  7. password: 'Add your password',
  8. host: 'imap.gmail.com',
  9. port: 993,
  10. tls: true
  11. });
  12.  
  13. function openInbox(cb) {
  14. imap.openBox('INBOX', true, cb);
  15. }
  16.  
  17. imap.once('ready', function() {
  18. openInbox(function(err, box) {
  19. if (err) throw err;
  20. imap.search([ 'UNSEEN', ['SINCE', 'March 5, 2019'] ], function(err, results) {
  21. if (err) throw err;
  22. var f = imap.fetch(results, { bodies: '' });
  23. f.on('message', function(msg, seqno) {
  24. console.log('Message #%d', seqno);
  25. var prefix = '(#' + seqno + ') ';
  26. msg.on('body', function(stream, info) {
  27. console.log(prefix + 'Body');
  28. stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt'));
  29. });
  30. msg.once('attributes', function(attrs) {
  31. console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
  32. });
  33. msg.once('end', function() {
  34. console.log(prefix + 'Finished');
  35. });
  36. });
  37. f.once('error', function(err) {
  38. console.log('Fetch error: ' + err);
  39. });
  40. f.once('end', function() {
  41. console.log('Done fetching all messages!');
  42. imap.end();
  43. });
  44. });
  45. });
  46. });
  47.  
  48. imap.once('error', function(err) {
  49. console.log(err);
  50. });
  51.  
  52. imap.once('end', function() {
  53. console.log('Connection ended');
  54. });
  55.  
  56. imap.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement