Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: mime.extension is not a function #76

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
node_modules/
npm-debug.log
attachments/
4 changes: 3 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copyright (c) 2012-2014 Chirag Jain
Copyright (c) 2012-2014 Chirag Jain (mail-listener2)
Pranav Dakshinamurthy 2014-2018 (mail-listener4)
Matej Malicek 2019 (mail-listener5)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
257 changes: 128 additions & 129 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,147 @@
/**@module mail-listener5
* @author Matej Malicek <[email protected]>
* @version 1.0.0
* @date 4 March 2019
*/

// Require statements
var Imap = require('imap');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var MailParser = require("mailparser").MailParser;
var fs = require("fs");
var simpleParser = require('mailparser').simpleParser;
var fs = require('fs');
var path = require('path');
var async = require('async');

module.exports = MailListener;

function MailListener(options) {
this.markSeen = !! options.markSeen;
this.mailbox = options.mailbox || "INBOX";
if ('string' === typeof options.searchFilter) {
this.searchFilter = [options.searchFilter];
} else {
this.searchFilter = options.searchFilter || ["UNSEEN"];
}
this.fetchUnreadOnStart = !! options.fetchUnreadOnStart;
this.mailParserOptions = options.mailParserOptions || {};
if (options.attachments && options.attachmentOptions && options.attachmentOptions.stream) {
this.mailParserOptions.streamAttachments = true;
class MailListener extends EventEmitter {
constructor(options) {
super();
this.markSeen = !! options.markSeen;
this.mailbox = options.mailbox || 'INBOX';
if ('string' === typeof options.searchFilter)
{
this.searchFilter = [options.searchFilter];
}
else
{
this.searchFilter = options.searchFilter || ['UNSEEN'];
}
this.fetchUnreadOnStart = !! options.fetchUnreadOnStart;
this.mailParserOptions = options.mailParserOptions || {};
if (options.attachments && options.attachmentOptions && options.attachmentOptions.stream)
{
this.mailParserOptions.streamAttachments = true;
}
this.attachmentOptions = options.attachmentOptions || {};
this.attachments = options.attachments || false;
this.attachmentOptions.directory = (this.attachmentOptions.directory ? this.attachmentOptions.directory : '');
this.imap = new Imap({
xoauth2: options.xoauth2,
user: options.username,
password: options.password,
host: options.host,
port: options.port,
tls: options.tls,
tlsOptions: options.tlsOptions || {},
connTimeout: options.connTimeout || null,
authTimeout: options.authTimeout || null,
debug: options.debug || null
});
this.imap.once('ready', this.imapReady.bind(this));
this.imap.once('close', this.imapClose.bind(this));
this.imap.on('error', this.imapError.bind(this));
}
this.attachmentOptions = options.attachmentOptions || {};
this.attachments = options.attachments || false;
this.attachmentOptions.directory = (this.attachmentOptions.directory ? this.attachmentOptions.directory : '');
this.imap = new Imap({
xoauth2: options.xoauth2,
user: options.username,
password: options.password,
host: options.host,
port: options.port,
tls: options.tls,
tlsOptions: options.tlsOptions || {},
connTimeout: options.connTimeout || null,
authTimeout: options.authTimeout || null,
debug: options.debug || null
});

this.imap.once('ready', imapReady.bind(this));
this.imap.once('close', imapClose.bind(this));
this.imap.on('error', imapError.bind(this));
}

util.inherits(MailListener, EventEmitter);

MailListener.prototype.start = function() {
this.imap.connect();
};
start() {
this.imap.connect();
}

MailListener.prototype.stop = function() {
this.imap.end();
};
stop() {
this.imap.connect();
}

function imapReady() {
var self = this;
this.imap.openBox(this.mailbox, false, function(err, mailbox) {
if (err) {
self.emit('error', err);
} else {
self.emit('server:connected');
if (self.fetchUnreadOnStart) {
parseUnread.call(self);
imapReady() {
this.imap.openBox(this.mailbox, false, (error, mailbox) => {
if (error)
{
this.emit('error', error);
}
var listener = imapMail.bind(self);
self.imap.on('mail', listener);
self.imap.on('update', listener);
}
});
}

function imapClose() {
this.emit('server:disconnected');
}
else
{
this.emit('server:connected');
this.emit('mailbox', mailbox);
if (this.fetchUnreadOnStart)
{
this.parseUnread.call(this);
}
let listener = this.imapMail.bind(this);
this.imap.on('mail', listener);
this.imap.on('update', listener);
}
});
}

function imapError(err) {
this.emit('error', err);
}
imapClose() {
this.emit('server:disconnected');
}

function imapMail() {
parseUnread.call(this);
}
imapError(error) {
this.emit('error', error);
}

function parseUnread() {
var self = this;
this.imap.search(self.searchFilter, function(err, results) {
if (err) {
self.emit('error', err);
} else if (results.length > 0) {
async.each(results, function( result, callback) {
var f = self.imap.fetch(result, {
bodies: '',
markSeen: self.markSeen
});
f.on('message', function(msg, seqno) {
var parser = new MailParser(self.mailParserOptions);
var attributes = null;
var emlbuffer = new Buffer('');
imapMail() {
this.parseUnread.call(this);
}

parser.on("end", function(mail) {
mail.eml = emlbuffer.toString('utf-8');
if (!self.mailParserOptions.streamAttachments && mail.attachments && self.attachments) {
async.each(mail.attachments, function( attachment, callback) {
fs.writeFile(self.attachmentOptions.directory + attachment.generatedFileName, attachment.content, function(err) {
if(err) {
self.emit('error', err);
callback()
} else {
attachment.path = path.resolve(self.attachmentOptions.directory + attachment.generatedFileName);
self.emit('attachment', attachment);
callback()
}
});
}, function(err){
self.emit('mail', mail, seqno, attributes);
callback()
});
} else {
self.emit('mail',mail,seqno,attributes);
}
});
parser.on("attachment", function (attachment) {
self.emit('attachment', attachment);
parseUnread() {
let self = this;
self.imap.search(self.searchFilter, (error, results) => {
if (error)
{
self.emit('error', err);
}
else if (results.length > 0)
{
async.each(results, (result, callback) => {
let f = self.imap.fetch(result, {
bodies: '',
markSeen: self.markSeen
});
msg.on('body', function(stream, info) {
stream.on('data', function(chunk) {
emlbuffer = Buffer.concat([emlbuffer, chunk]);
});
stream.once('end', function() {
parser.write(emlbuffer);
parser.end();
f.on('message', (msg, seqno) => {
msg.on('body', async (stream, info) => {
let parsed = await simpleParser(stream);
self.emit('mail', parsed, seqno);
self.emit('headers', parsed.headers, seqno);
self.emit('body', {html: parsed.html, text: parsed.text, textAsHtml: parsed.textAsHtml}, seqno);
if (parsed.attachments.length>0)
{
for (let att of parsed.attachments)
{
if (self.attachments)
{
await fs.writeFile(`${self.attachmentOptions.directory}${att.filename}`, att.content, (error) =>{
self.emit('error', error);
});
self.emit('attachment', att, `${self.attachmentOptions.directory}${att.filename}`, seqno);
}
else
{
self.emit('attachment', att, null, seqno);
}
}
}
});
});
msg.on('attributes', function(attrs) {
attributes = attrs;
f.once('error', (error) => {
self.emit('error', error);
});
}, (error) => {
if (error)
{
self.emit('error', error);
}
});
f.once('error', function(err) {
self.emit('error', err);
});
}, function(err){
if( err ) {
self.emit('error', err);
}
});
}
});
}
}
});
}
};
module.exports.MailListener = MailListener;
Loading