Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var Imap = require('imap');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var MailParser = require("mailparser").MailParser;
var fs = require("fs");
var path = require('path');
var async = require('async');
var Imap = require('imap'),
util = require('util'),
EventEmitter = require('events').EventEmitter,
MailParser = require("mailparser").MailParser,
fs = require("fs"),
path = require('path'),
async = require('async');

module.exports = MailListener;

Expand Down Expand Up @@ -82,7 +82,8 @@ function parseUnread() {
if (err) {
self.emit('error', err);
} else if (results.length > 0) {
async.each(results, function( result, callback) {
var remaining = results.length;
async.each(results, function(result, callback) {
var f = self.imap.fetch(result, {
bodies: '',
markSeen: self.markSeen
Expand All @@ -93,23 +94,27 @@ function parseUnread() {

parser.on("end", function(mail) {
if (!self.mailParserOptions.streamAttachments && mail.attachments && self.attachments) {
async.each(mail.attachments, function( attachment, callback) {
async.each(mail.attachments, function(attachment, callback) {
fs.writeFile(self.attachmentOptions.directory + attachment.generatedFileName, attachment.content, function(err) {
if(err) {
if (err) {
self.emit('error', err);
callback()
callback();
} else {
attachment.path = path.resolve(self.attachmentOptions.directory + attachment.generatedFileName);
self.emit('attachment', attachment);
callback()
callback();
}
});
}, function(err){
self.emit('mail', mail, seqno, attributes);
callback()
});
} else {
self.emit('mail',mail,seqno,attributes);
self.emit('mail', mail, seqno, attributes);
}
remaining = remaining - 1;
if (remaining == 0) {
self.emit('done');
}
});
parser.on("attachment", function (attachment) {
Expand All @@ -126,10 +131,12 @@ function parseUnread() {
self.emit('error', err);
});
}, function(err){
if( err ) {
if(err) {
self.emit('error', err);
}
});
} else {
self.emit('done');
}
});
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ mailListener.on("attachment", function(attachment){
console.log(attachment.path);
});

mailListener.on("done", function(attachment){
console.log("No more messages");
});


// it's possible to access imap object from node-imap library for performing additional actions. E.x.
mailListener.imap.move(:msguids, :mailboxes, function(){})

Expand Down