Guest User

Untitled

a guest
Oct 18th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. var Imap = require('imap'),
  2. inspect = require('util').inspect;
  3.  
  4. var imap = new Imap({
  5. user: 'yourname@gmail.com',
  6. password: 'yourpassword',
  7. host: 'imap.gmail.com',
  8. port: 993,
  9. tls: true
  10. });
  11.  
  12. function openInbox(cb) {
  13. imap.openBox('[Gmail]/All Mail', false, cb);
  14. }
  15.  
  16. imap.once('ready', function () {
  17. openInbox(function (err, box) {
  18. if (err) throw err;
  19.  
  20. var f = imap.seq.fetch('1:' + box.messages.total, {
  21. bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE X-Uniform-Type-Identifier)',
  22. struct: true
  23. });
  24.  
  25. f.on('message', function (msg, seqno) {
  26.  
  27. var isNote = false;
  28.  
  29. console.log('Message #%d', seqno);
  30. var prefix = '(#' + seqno + ') ';
  31. msg.on('body', function (stream, info) {
  32. var buffer = '';
  33. stream.on('data', function (chunk) {
  34. buffer += chunk.toString('utf8');
  35. });
  36. stream.once('end', function () {
  37. var headers = Imap.parseHeader(buffer);
  38.  
  39. if (headers.hasOwnProperty('x-uniform-type-identifier') && headers['x-uniform-type-identifier'].length > 0 && headers['x-uniform-type-identifier'][0] == 'com.apple.mail-note') {
  40. console.log(Imap.parseHeader(buffer));
  41.  
  42. isNote = true;
  43.  
  44. }
  45.  
  46. });
  47.  
  48. });
  49.  
  50. msg.once('attributes', function (attrs) {
  51. if (isNote) {
  52.  
  53. imap.addLabels(attrs.uid, 'Notes', function (err) {
  54. if (err) console.log('setLabels error: ', err);
  55. });
  56.  
  57.  
  58. console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
  59. }
  60.  
  61. });
  62.  
  63. msg.once('end', function () {
  64. //console.log(prefix + 'Finished');
  65. });
  66.  
  67. });
  68.  
  69. f.once('error', function (err) {
  70. console.log('Fetch error: ' + err);
  71. });
  72.  
  73. f.once('end', function () {
  74. console.log('Done fetching all messages!');
  75. imap.closeBox(function (err) {
  76. if (err) console.log(err);
  77. imap.end();
  78. });
  79.  
  80.  
  81. });
  82. });
  83. });
  84.  
  85. imap.once('error', function (err) {
  86. console.log(err);
  87. });
  88.  
  89. imap.once('end', function () {
  90. console.log('Connection ended');
  91. });
  92.  
  93. imap.connect();
Add Comment
Please, Sign In to add comment