使用node-imap和mailparser解析不正确的gmail

时间:2015-08-17 22:16:59

标签: node.js email gmail gmail-imap email-parsing

我正在编写一个Node.js脚本,该脚本使用node-imap从我的gmail收件箱中检索未读的电子邮件,使用mailparser解析它们,然后使用已解析的电子邮件进行处理。我遇到了一个问题,即收到的原始电子邮件似乎没有被mailparser正确解析。我不确定在调用node-imap或mailparser时我是否做错了什么,或者由于某种原因电子邮件本身的格式是否错误。我已经包含了我运行的代码以及生成的输出。

var Imap = require("imap"),
    MailParser = require("mailparser").MailParser,
    Promise = require("bluebird"),
    request = require("request-promise").defaults({jar: true}),
    log = require("winston"),
    _ = require("underscore"),
    config = require("config").jobs;

var logConfig = _.clone(config.logConfig.email);
if (process.env.LOG_DIR) {
    logConfig.filename = process.env.LOG_DIR + "/" + logConfig.filename;
}
log.add(log.transports.File, logConfig || config.logConfig);

Promise.longStackTraces();

var imap = new Imap(config.emailConfig);
Promise.promisifyAll(imap);

imap.once("ready", execute);
imap.once("error", function (err) {
    log.error("Connection error: " + err.stack);
});
imap.connect();

function execute() {
    imap.openBoxAsync("INBOX", false)
        .then(function () {
            return imap.searchAsync(["UNSEEN"]);
        })
        .then(function (results) {
            var f = imap.fetch(results, {bodies: ["HEADER.FIELDS (FROM SUBJECT)", "TEXT"]});
            f.on("message", processMessage);
            f.once("error", function (err) {
                return Promise.reject(err);
            });
            f.once("end", function () {
                log.info("Done fetching all unseen messages.");
                imap.end();
            });
        })
        .catch(function (err) {
            log.error("Error fetching messages: " + err.stack);
            imap.end();
        });
}

function processMessage(msg, seqno) {
    log.info("Processing msg #" + seqno);

    var parser = new MailParser();
    parser.on("headers", function(headers) {
       log.info("Header: " + JSON.stringify(headers));
    });
    parser.on("end", function(msg) {
        log.info("From: " + msg.from);
        log.info("Subject: " + msg.subject);
        log.info("Text: " + msg.text);
        log.info("Html: " + msg.html);
    });

    msg.on("body", function (stream) {
        stream.on("data", function (chunk) {
            parser.write(chunk.toString("utf8"));
        });
    });
    msg.once("end", function () {
        log.info("Finished msg #" + seqno);
        parser.end();
    });
}

输出:

info: Processing msg #1
info: Finished msg #1
info: Done fetching all unseen messages.
info: Header: {"--001a11339690da942a051d866a04":"","content-type":"text/plain; charset=UTF-8"}
info: From: undefined
info: Subject: undefined
info: Text: Test app mail body!

- Jared

--001a11339690da942a051d866a04
Content-Type: text/html; charset=UTF-8

<div dir="ltr"><div>Test app mail body!<br><br></div>- Jared<br></div>

--001a11339690da942a051d866a04--
Subject: Here is a test appmail email
From: Jared Wolinsky <jared.wolinsky@gmail.com>


info: Html: undefined

1 个答案:

答案 0 :(得分:7)

您正在获取电子邮件的原始正文mailparser期待完整的电子邮件,标题正文。要获取它,只需指定一个空字符串''而不是'TEXT'