Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var Imap = require('imap'),
  4. inspect = require('util').inspect;
  5.  
  6. var imap = new Imap({
  7. password: 'teste',
  8. host: 'imap.pling.net.br',
  9. port: 993,
  10. tls: true,
  11. tlsOptions: { rejectUnauthorized: false }
  12. });
  13.  
  14. function openInbox (cb) {
  15.  
  16. imap.openBox('INBOX', true, cb);
  17. }
  18.  
  19. imap.once('ready', function() {
  20.  
  21. openInbox(function(err, box) {
  22.  
  23. if (err) throw err;
  24.  
  25. console.log('Estatisticas da caixa: ', box);
  26.  
  27. imap.search( ['ALL', ['UID', 6]], function(error, uid){
  28.  
  29. var f = imap.fetch(`${uid}`, {
  30. bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)'
  31. });
  32.  
  33. f.on('message', function(msg, seqno) {
  34.  
  35. var prefix = '(#' + seqno + ') ';
  36. msg.on('body', function(stream, info) {
  37.  
  38. var buffer = '';
  39. stream.on('data', function(chunk) {
  40. buffer += chunk.toString('utf8');
  41. });
  42. stream.once('end', function() {
  43.  
  44. console.log('Message #%d', seqno, "\n");
  45. console.log(prefix, 'Parsed header: ' + inspect(Imap.parseHeader(buffer), "\n\n"));
  46. });
  47. });
  48.  
  49. msg.once('attributes', function(attrs) {
  50. console.log('uid: ', attrs.uid, 'modseq: ', attrs.modseq );
  51. });
  52.  
  53. msg.once('end', function() {
  54. console.log(prefix, 'Finished', "\n\n");
  55. });
  56. });
  57.  
  58. f.once('error', function(err) {
  59. console.log('Fetch error: ' + err);
  60. });
  61.  
  62. f.once('end', function() {
  63. console.log('Done fetching all messages!');
  64. imap.end();
  65. });
  66. });
  67.  
  68.  
  69. });
  70.  
  71. /**
  72. * Return all boxes
  73. */
  74. imap.getBoxes( function(err, box){
  75. console.log( 'Box: ', box, "\n" );
  76. });
  77.  
  78. });
  79.  
  80. imap.once('error', function(err) {
  81. console.log(err);
  82. });
  83.  
  84. imap.once('end', function() {
  85. console.log('Connection ended');
  86. });
  87.  
  88. imap.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement