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

Incorrect gmail parsing with node-imap and mailparser

我正在编写一个 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

您正在获取电子邮件的原始 bodymailparser 期待 完整 电子邮件,headers body 两者。要获取它,只需指定一个空字符串 '' 而不是 'TEXT'.