【问题标题】:Haraka Smtp Server ( Editing Outbound EMail Body Content)Haraka Smtp 服务器(编辑出站电子邮件正文内容)
【发布时间】:2016-09-28 08:05:00
【问题描述】:

我正在尝试为 haraka 一​​个 nodejs 驱动的 smtp 服务器编写一个自定义插件。我想在邮件正文中添加一些文本。到目前为止,这是我的代码。

var utils  = require('./utils');
var util   = require('util');
exports.hook_data = function (next, connection) 
{
    connection.transaction.parse_body = true; 
    next();
}

exports.hook_data_post = function (next,connection)
{
    var plugin = this ;
    plugin.loginfo(connection.transaction.body.bodytext);
    var pos =connection.transaction.body.bodytext.indexOf('\<\/body\>');
    connection.transaction.body.bodytext = connection.transaction.body.bodytext.splice(pos-1, 0,  '<p>add this paragraph to the existing body.</p>  \r \n');

    plugin.loginfo(connection.transaction.body.bodytext);

    next();
}

String.prototype.splice = function(idx, rem, str) 
{
    return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
exports.hook_queue_outbound = function(next,connection)
{
    var plugin = this;
    plugin.loginfo(connection.transaction.body.bodytext);
    next();
}

当插件在这里运行时,它会打印到日志中。

旧身体日志信息:

[INFO] [-] [add_some_data] <html>
    <body>
    olddata
   <p>add this paragraph to the existing body.</p>  \r
 </body>
</html>

新的身体日志:

[INFO] [-] [add_some_data] <html>
    <body>
    olddata
   <p>add this paragraph to the existing body.</p>  \r
 </body>
</html>

我想知道的是为什么它没有在外发电子邮件中包含数据。

如您所见,我什至尝试将消息正文记录在“hook_queue_outbound”中,这是一个稍后调用 hook_post_data 的钩子,我可以看到编辑后的结果。但在接收端,我收到了旧电子邮件。 我犯了一些愚蠢的错误,如果得到指示,我将不胜感激。
谢谢。

【问题讨论】:

    标签: javascript node.js smtp html-email haraka


    【解决方案1】:

    好吧,伙计们,我很努力,我终于做到了。以防其他人将来可能会发现它有帮助,所以我发布了我如何完成它。 haraka add_body_filter 中有一个内置帮助器,我使用它..:)
    干杯

    exports.hook_data = function (next, connection) 
    { 
       var plugin = this;
       connection.transaction.parse_body = true; 
       connection.transaction.add_body_filter(/text\/(plain|html)/, function (ct, enc, buff)
       {
            var buf = buff.toString('utf-8');
            var pos = buf.indexOf('\<\/body\>');
            buf = buf.splice(pos-1, 0,  '<p>add this paragraph to the existing body.</p>');
            return new Buffer(buf);
        });
        next();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 2019-06-19
      • 2015-02-25
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      相关资源
      最近更新 更多