如何在 Haraka 上解析电子邮件?

问题描述 投票:0回答:4

我一直在探索haraka (http://haraka.github.io)并且我已经成功地将它安装在我的linux机器上。我想知道是否有关于使用 haraka 解析电子邮件元标头和内容正文的好教程。我查过他们的手册,但找不到。任何关于如何做到这一点的想法/建议将不胜感激。谢谢。

这是我检索电子邮件正文的脚本:

var winston = require('winston');
exports.hook_data = function (next, connection) {
    winston.log('info', '----------------------------------------');
    winston.log('info', 'hook_data');
    // enable mail body parsing
    connection.transaction.parse_body = true;
    winston.log('info', "body="+connection.transaction.body);
    winston.log('info', "mail_from="+connection.transaction.mail_from);
    next();
}

输出:

{"level":"info","message":"----------------------------------------","timestamp":"2015-01-12T07:16:28.216Z"}
{"level":"info","message":"hook_data","timestamp":"2015-01-12T07:16:28.217Z"}
{"level":"info","message":"body=null","timestamp":"2015-01-12T07:16:28.218Z"}
{"level":"info","message":"[email protected]","timestamp":"2015-01-12T07:16:28.218Z"}

正如您所看到的,主体包含一个空值。

javascript node.js email smtp mail-server
4个回答
3
投票

对我来说这很有效

connection.transaction.body.children[1].bodytext
exports.hook_data = function (next, connection) {
    connection.transaction.parse_body = true;
    this.loginfo("connection.transaction.parse_body");
    next();
}

exports.hook_data_post = function (next, connection) {
    this.loginfo("connection.transaction.body.bodytext");
    this.loginfo(connection.transaction.body.children[1].bodytext);
    next();
}

2
投票

似乎需要在数据到达之前设置 transaction.parse_body - 因此请在“data”挂钩(在 DATA 命令中调用)中或更早地进行设置:

exports.hook_data = function (next, connection) {
    connection.transaction.parse_body = true;
    next();
}

然后您可以从“data_post”(在数据结束标记处调用)挂钩读取正文:

exports.hook_data_post = function (next, connection) {
    this.loginfo(connection.transaction.body.bodytext);
    next();
}

1
投票

您可以使用以下代码检索电子邮件正文

connection.transaction.body.body_text_encoded
connection.transaction.body.bodytext

它的代码很容易理解。您可以阅读并按照您的要求去做。


0
投票

之前的答案遗漏了一些重要的细节。我已经演示了如何在数据挂钩中获取各种标头。

电子邮件正文可以有多个子项。

每个钩子都需要调用回调(下一个)以继续处理电子邮件管道中的下一个阶段。

    exports.hook_data = function (next, connection) {
        // enable mail body parsing - should be enabled in hook_data
        connection.transaction.parse_body = true;
    
        // get a decoded copy of a header, e.g. From
        const from_header = connection.transaction.header.get_decoded('From');
        connection.loginfo(this, `From: ${from_header}`);
        
        // get a header that doesn't need to be decoded:
        const msg_id = connection.transaction.header.get('Message-ID');
        
        // get multiple headers, e.g. Received:
        const received = connection.transaction.header.get_all('received');

        next();
    }

    exports.hook_data_post = function (next, connection) {
        // confirm body has text
        if(connection.transaction.body.bodytext) {
            this.examine_body(connection.transaction.body.bodytext);
        }

        next();
    }

    exports.examine_body = function (body) {
        if (!body.bodytext) return;
    
        this.loginfo(body.bodytext);

        // recursively get all of the children
        if (body.children?.length) {
            for (const child of body.children) {
                this.examine_body(child);
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.