revisionfour
8/2/2013 - 6:40 PM

Email handling

Email handling

var Imap = require('imap');

var inspect = require('util').inspect;

var fs = require('fs'), fileStream;

var MailParser = require("mailparser").MailParser;
// var mailparser = new MailParser();

function getReceipts(email, password, callback){

	var hostname = 'imap.gmail.com';

	var imap = new Imap({
    user: email,
    password: password,
    host: hostname,
    port: 993,
    tls: true,
    tlsOptions: { rejectUnauthorized: false }
  });

  function openInbox(cb) {
	  imap.openBox('INBOX', true, cb);
	}

	// mailparser.on("end", function(mail_object){
 //    console.log("Subject:", mail_object.subject);
	// });

	// Receipts Array
	var receipts = [];

	imap.once('ready', function(){
		openInbox(function(err, box) {
	    if (err) throw err;

	    imap.search([ 'All', ['SINCE', 'Jan 1, 1990'] ], function(err, results) {
		    if (err) throw err;

		    var f = imap.fetch(results, { bodies: '' });

		    f.on('message', function(msg, seqno) {

		      // console.log('Message #%d', seqno);

		      var prefix = '(#' + seqno + ') ';

		      var emailFilename = 'public/receipts/msg-' + seqno + '-body.txt';

		      var mailparser = new MailParser();

		      mailparser.on("end", function(mail_object){
		      	// console.log('Filename: ' + emailFilename);
				    // console.log("Subject:", mail_object.subject);
				    // console.log(mail_object.html);

				    receipts.push(mail_object.html);
					});

		      msg.on('body', function(stream, info) {
		        // console.log(prefix + 'Body');
		        // Create a file somewhere
		        // console.log('STARTING: ' + emailFilename);




		        // stream.pipe(fs.createWriteStream(emailFilename));
		        // console.log(info);

		      });

		      // msg.once('attributes', function(attrs) {
		      //   console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
		      // });

		      msg.once('end', function() {
		        // console.log(prefix + 'Finished');

		        // console.log('DONE: ' + emailFilename);

		        fs.createReadStream(emailFilename).pipe(mailparser);


		      });

		    });

		    f.once('error', function(err) {
		      console.log('Fetch error: ' + err);
		    });

		    f.once('end', function() {
		      console.log('Done fetching all messages!');
		      imap.end();
		    });

		  });

	  });
  });

		
  // Error connecting
  imap.once('error', function(err) {
    console.log(err);
  });

  imap.once('end', function() {
    console.log('Connection ended');
    // imap.end();
    callback(receipts);
  });

	imap.connect();


}

// Main route for receipt viewer
module.exports = function(app){

	// Main site for the email receipt viewer
	app.get('/',function(req, res){
		res.sendfile('public/index.html');
	});

	app.get('/getreceipts',function(req, res){
		var email = req.param('email');
		var password = req.param('password');

		console.log(email);
		console.log(password);

		getReceipts(email, password, function(receipts){

			// res.send(receipts);

			res.send({receipts: receipts});

			// res.send('Got it!');
		});

	});

};