Haraka Smtp Server ( Editing Outbound EMail Body Content)

1.1k Views Asked by At

i am trying to write a custom plugin for haraka a nodejs powered smtp server. i want to add some text to the mailbody. here is my code so far.

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();
}

when the plugin runs here is the what it prints to the log.

Old Body Loginfo:

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

New Body Log:

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

What i want to know is why it did not include the data inside the outgoing email.

as you can see i even tried to log the message body inside the "hook_queue_outbound" a hook that is called later to hook_post_data and i can see the result edited . but at the receiving end i get the old email. i am doing some stupid mistake and i will highly appreciate if given a direction.
Thank You.

1

There are 1 best solutions below

0
On

Ok Fellows i struggled and i finally did it. incase someone else might find it helpful in future so i am posting how i accomplish it. there is a builtin helper in haraka add_body_filter i used it..:)
cheers

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();
}