The subject content is undefined

I have a small problem using mailparser. When I'm trying to see the subject content it says that is undefined. My code looks something like this :

var fs = require("fs");
var socket = fs.createWriteStream('./outputmail.eml');
socket.on("end",function()
{
    connection.loginfo("end------->");
});
connection.transaction.message_stream.pipe(socket, {}); // haraka related
var mailparser = new MailParser();
mailparser.on("end", function(mail_object){
    connection.loginfo("----------SUBJECT-------------------->:", mail_object.subject);
});
fs.createReadStream("./outputmail.eml").pipe(mailparser);

I'm using mailparser in Haraka (http://haraka.github.io) . The whole point doing this, is that I want to parse every mail I receive and add a banner/image/whatever into the mail's body. If you have any idea why this message appears , please let me know.

If that is the code it is likely that you start reading the file before it is written. As the libraries both support streams you should be able to pipe the message_stream directly to mailparser, ie.

var mailparser = new MailParser();
mailparser.on("end", function(mail_object) {
  console.log(mail_object);
});
connection.transaction.message_stream.pipe(mailparser, {});

Another reason could naturally be that there just isn't a subject in the email.