2016-05-30 37 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(); 
} 
相關問題