Skip to content

Commit

Permalink
Merge pull request #2 from inishchith/email-module
Browse files Browse the repository at this point in the history
 Adding receiver and config file
  • Loading branch information
meets2tarun authored Jun 5, 2018
2 parents 66c439d + 5394774 commit f8cce34
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions email/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"user": "your_email",
"password": "your_password",
"host": "imap.gmail.com",
"port": 993,
"tls": true
}
54 changes: 54 additions & 0 deletions email/receiver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var config = require('./config');

var Imap = require('imap'),
inspect = require('util').inspect;

var imap = new Imap(config);

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

imap.once('ready', function() {
var fs = require('fs'), fileStream;

openInbox(function(err, box) {
if (err) throw err;
// test for May 28, 2018
imap.search([ 'UNSEEN', ['SINCE', 'May 28, 2018'] ], 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 + ') ';
msg.on('body', function(stream, info) {
console.log(prefix + 'Body');
stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt'));
});
msg.once('attributes', function(attrs) {
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
});
});
});

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

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

imap.connect();

0 comments on commit f8cce34

Please sign in to comment.