nodemailer / mailparser

Decode mime formatted e-mails
Other
1.59k stars 281 forks source link

Error Handling in Case of Encoding Problems #151

Closed ip6li closed 6 years ago

ip6li commented 8 years ago

Version: mailparser@0.6.1 iconv@2.2.1 nodejs: v6.3.1

Log is filled with messages like:

    Jul 23 20:59:52 trashmail node[25726]: Error: Conversion from CP-850 to UTF-8//TRANSLIT//IGNORE is not supported.
    Jul 23 20:59:52 trashmail node[25726]: at new Iconv (/home/trashmail/trashmail/node_modules/iconv/lib/iconv.js:51:11)
    Jul 23 20:59:52 trashmail node[25726]: at convertIconv (/home/trashmail/trashmail/node_modules/encoding/lib/encoding.js:76:13)
    Jul 23 20:59:52 trashmail node[25726]: at convert (/home/trashmail/trashmail/node_modules/encoding/lib/encoding.js:39:22)
    Jul 23 20:59:52 trashmail node[25726]: at Object.module.exports.mimeFunctions.mimeDecode (/home/trashmail/trashmail/node_modules/mimelib/lib/mimelib.js:238:16)
    Jul 23 20:59:52 trashmail node[25726]: at Object.module.exports.mimeFunctions.decodeQuotedPrintable (/home/trashmail/trashmail/node_modules/mimelib/lib/mimelib.js:254:21)
    Jul 23 20:59:52 trashmail node[25726]: at Object.module.exports.decodeQuotedPrintable (/home/trashmail/trashmail/node_modules/mimelib/lib/mimelib.js:99:54)
    Jul 23 20:59:52 trashmail node[25726]: at MailParser._finalizeContents (/home/trashmail/trashmail/node_modules/mailparser/lib/mailparser.js:957:53)
    Jul 23 20:59:52 trashmail node[25726]: at MailParser._processStateBody (/home/trashmail/trashmail/node_modules/mailparser/lib/mailparser.js:375:26)
    Jul 23 20:59:52 trashmail node[25726]: at MailParser._process (/home/trashmail/trashmail/node_modules/mailparser/lib/mailparser.js:233:22)
    Jul 23 20:59:52 trashmail node[25726]: at runCallback (timers.js:570:20)

Does mailparser support some type of error handling?

mailparser.on("end", function (mail_object) {
...
}

should get an error object, if mailparser detects a problem, so application can act on error.

function where this happens:

function renderMessage(realTo, rawMessage, cb) {
    var mailparser = new MailParser();
    var uid = rawMessage.uid;

        mailparser.on("end", function (mail_object) {
            var from = ""; 
            var rcpt = ""; 

            if (typeof mail_object.from !== "undefined") {
                var fromList = extractNames(mail_object.from);
                for (var iFrom=0; iFrom<fromList.length; iFrom++) {
                    from += fromList[iFrom] + " ";
                }
            }

            var rcptList = []; 
            if (typeof mail_object.to !== "undefined") {
                Array.prototype.push.apply(rcptList, extractNames(mail_object.to));
            }
            if (typeof mail_object.cc !== "undefined") {
                Array.prototype.push.apply(rcptList, extractNames(mail_object.cc));
            }

            var x_original = mail_object.headers["x-original-to"];
            if ((typeof x_original !== "undefined") &&
                (rcptList.indexOf(x_original)==-1)) {
                rcptList.push(x_original);
            }

            for (var iRcpt=0; iRcpt<rcptList.length; iRcpt++) {
                rcpt += rcptList[iRcpt] + " ";
            }

            var re = new RegExp(realTo.email+".*@"+realTo.domain);
            if (rcpt.match(re) && (typeof realTo.password !== "undefined")) {
                var html = makeHtml(from, rcpt, uid, realTo.password, mail_object);
                cb(html);
            }
        }); 

        mailparser.write(rawMessage.content);
        mailparser.end();
    }

That function get a mail object from npm imap module (it read mail from a Dovecot server), checks for a specific email address in list of recipients, renders a message and send result to a callback. If error situation while parsing cannot be handled, this may yield into problems while processing following steps.

LionNatsu commented 8 years ago

I have this problem too.

Error: Encoding not recognized: 'ISO-2022-JP' (searched as: 'iso2022jp')
    at Object.getCodec (/home/lion/repos/aosc/email-bot/node_modules/iconv-lite/lib/index.js:102:23)
    at Object.getDecoder (/home/lion/repos/aosc/email-bot/node_modules/iconv-lite/lib/index.js:118:23)
    at Object.decode (/home/lion/repos/aosc/email-bot/node_modules/iconv-lite/lib/index.js:36:25)
    at convertIconvLite (/home/lion/repos/aosc/email-bot/node_modules/encoding/lib/encoding.js:91:26)
    at convert (/home/lion/repos/aosc/email-bot/node_modules/encoding/lib/encoding.js:51:22)
    at Object.decodeBase64 (/home/lion/repos/aosc/email-bot/node_modules/mimelib/lib/mimelib.js:248:16)
    at Object.decodeMimeWord (/home/lion/repos/aosc/email-bot/node_modules/mimelib/lib/mimelib.js:321:25)
    at Object.module.exports.decodeMimeWord (/home/lion/repos/aosc/email-bot/node_modules/mimelib/lib/mimelib.js:55:41)
    at MailParser.<anonymous> (/home/lion/repos/aosc/email-bot/node_modules/mailparser/lib/mailparser.js:1388:24)
    at RegExp.[Symbol.replace] (native)

I think it should handle this (when it calls to mimelib) and raise an error event.