fs.readFile('./test.txt', 'utf8', function (err, data) { if (err) { return console.log(err); } // setup an event listener when the parsing finishes mailparser.on("end", function (mail_object) { console.log("From:", mail_object.from); console.log("Subject:", mail_object.subject); // Hello world! // console.log("Text body:", mail_object.text); // How are you today? var osTicketAttachments = ; if (mail_object.attachments) { var tmpAttach = ""; var tmpFileName = ""; var tmpFileType =""; for (i = 0; i < mail_object.attachments.length; i++) { var tmpAttach = mail_object.attachments //var tmpFileName = '"'+ tmpAttach.fileName + '"'; //Name with Quotes var tmpFileName = tmpAttach.fileName //+ ''; tmpFileName.replace('"',"") var tmpFileType = "data:" + tmpAttach.contentType +";"+tmpAttach.transferEncoding +"," osTicketAttachments.push( { : tmpFileType + tmpAttach.content.toString("base64") }) // osTicketAttachments.push( JSON.parse ("'{tmpFileName :'"+ tmpFileType + tmpAttach.content.toString("base64") +"}")) tmpAttach = {}; tmpFileNameee = ""; tmpFileType=""; } } request({ url: 'http://helpdesk.test.com/osticket/upload/api/http.php/tickets.json', //URL to hit method: 'POST', headers: { 'X-API-Key': '7BFC4B9103930DD4B' }, //Lets post the following key/values as form json: { "name": mail_object.from.name, "email": mail_object.from.address, "subject": mail_object.subject, "message": "data/html;charset=utf-8;" + mail_object.html, "topicId": "1", "attachments": osTicketAttachments } }, function (error, response, body) { if (error) { console.log(error); } else { console.log(response.statusCode, body); } }); }); // send the email source to the parser mailparser.write(data); mailparser.end();});