Guest User

Untitled

a guest
Sep 17th, 2013
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.fetchMail = function(ind, done) {
  2.  
  3.   var imap = new Imap({
  4.     user: config.email.user,
  5.     password: config.email.password,
  6.     host: config.email.host,
  7.     tls: false,
  8.     tlsOptions: { rejectUnauthorized: false }
  9.   })
  10.  
  11.   imap.once('ready', function() {
  12.     imap.openBox('Inbox', true, function(err, box) {
  13.       if (err) return done(err)
  14.  
  15.       var imapFetch = imap.seq.fetch(ind, {
  16.         bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
  17.         struct: true
  18.       })
  19.  
  20.       imapFetch.on('message', function(msg, seqno) {
  21.         var body = ''
  22.           , header = ''
  23.           , parsedMsg = {}
  24.  
  25.         msg.on('body', function(stream, info) {
  26.           if (info.which === 'TEXT') {
  27.             stream.on('data', function(chunk) { body += chunk.toString('utf8') })
  28.             stream.once('end', function() { parsedMsg.body = body })
  29.           } else {
  30.             stream.on('data', function(chunk) { header += chunk.toString('utf8') })
  31.             stream.once('end', function() { parsedMsg.header = Imap.parseHeader(header) })
  32.           }
  33.         })
  34.  
  35.         msg.once('attributes', function(attrs) {
  36.           parsedMsg.attrs = attrs
  37.         })
  38.  
  39.         msg.once('end', function() {
  40.           done(null, parsedMsg)
  41.         })
  42.       })
  43.  
  44.       imapFetch.once('error', done)
  45.  
  46.     })
  47.   })
  48.  
  49.   imap.once('error', done)
  50.   imap.connect()
  51. }
Advertisement
Add Comment
Please, Sign In to add comment