2016-05-30 103 views
1

我正在尝试为haraka写一个自定义插件,它是一个nodejs供电的smtp服务器。我想给mailbody添加一些文本。这是我的代码到目前为止。Haraka 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(); 
} 

当插件运行在这里是它打印到日志。

老身体LOGINFO:

[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,我可以看到编辑的结果。但在接收端,我收到了旧的电子邮件。 我正在做一些愚蠢的错误,我会高度赞赏,如果给一个方向。
谢谢。

回答

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(); 
} 
相关问题