Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Create email record using .eml file attachment or retrieve .eml file content in ServiceNow

Sumanth34
Kilo Guru

How to create a email record using .eml file attachment or retrieve .eml file content in ServiceNow using MailParser() package or anyother available methods..

Please advise.

 

Thanks,

Sumanth Bolle

1 ACCEPTED SOLUTION

Sumanth34
Kilo Guru

Please try below custom script :

 

 

var Attachment = new GlideSysAttachment().getContentStream('8ea76e761b3da9540af564e0b24bcb71'); // attachment sys_id of .eml file
var reader = new GlideTextReader(Attachment);
var line = '';
var str = '';
while ((line = reader.readLine()) != null) {
    var htmlValue = "";

    if (GlideStringUtil.isBase64(line)) {

        htmlvalue = GlideStringUtil.unEscapeHTML(GlideStringUtil.base64Decode(line));

    } else {

        htmlvalue = GlideStringUtil.unEscapeHTML(line);

    }

    var check = htmlvalue.contains(" ");

    str += htmlvalue;

}



//gs.info(str);


var bodyStart = str.indexOf('<head>');

var bodyEnd = str.indexOf('</html>');


var bodyOutput = "";

if (bodyStart != -1) {

    bodyOutput = '<html>' + str.substring(bodyStart, bodyEnd).trim() + '</html>';

} else {

    bodyOutput = str;

}

gs.info(bodyOutput);

 

 

Please mark as helpful and let me know if there's any issue in formatting..

 

 

 

Best regards,

Sumanth Bolle

 

View solution in original post

1 REPLY 1

Sumanth34
Kilo Guru

Please try below custom script :

 

 

var Attachment = new GlideSysAttachment().getContentStream('8ea76e761b3da9540af564e0b24bcb71'); // attachment sys_id of .eml file
var reader = new GlideTextReader(Attachment);
var line = '';
var str = '';
while ((line = reader.readLine()) != null) {
    var htmlValue = "";

    if (GlideStringUtil.isBase64(line)) {

        htmlvalue = GlideStringUtil.unEscapeHTML(GlideStringUtil.base64Decode(line));

    } else {

        htmlvalue = GlideStringUtil.unEscapeHTML(line);

    }

    var check = htmlvalue.contains(" ");

    str += htmlvalue;

}



//gs.info(str);


var bodyStart = str.indexOf('<head>');

var bodyEnd = str.indexOf('</html>');


var bodyOutput = "";

if (bodyStart != -1) {

    bodyOutput = '<html>' + str.substring(bodyStart, bodyEnd).trim() + '</html>';

} else {

    bodyOutput = str;

}

gs.info(bodyOutput);

 

 

Please mark as helpful and let me know if there's any issue in formatting..

 

 

 

Best regards,

Sumanth Bolle