Export sys_email as .eml UI Action

pbusch
Tera Expert

 

Exporting sys_email from subPROD as .eml makes testing and demo-ing much easier.

 

I recommend the execellent work provided here:

servicenow-export-email/README.md at master · nhylated/servicenow-export-email · GitHub

~ "Breynia Disticha"
1 REPLY 1

midjoule
Kilo Sage

I tried to implement the above mentioned export functionality, but I couldn't make it work. The issue was with the attachments. So I adjusted it this way to make it work: attachments are appended as URLs to the end of the email. 

/**
 * Author/source: https://github.com/nhylated/servicenow-export-email
 */

var emlAttachmentSysId = exportEmailToEML(current);
action.setRedirectURL('sys_attachment.do?sys_id=' + emlAttachmentSysId);

/**
 * Generates the string content of the .EML file for a given email and creates the EML file as an attachment and returns its sys_id.
 * @Param {GlideRecord} sys_email_gr - A sys_email GlideRecord.
 * @return {string} sys_id of the .EML file attachment in the sys_attachment table.
 */
function exportEmailToEML(sys_email_gr) {
    var emlStr = '';

    // Add the content-type and body
    var obj = generateEMLBodyAndContentType(sys_email_gr);
    emlStr = sys_email_gr.headers;
    emlStr += '\n' + obj.contentType;
    emlStr = emlStr.replace(/[(\r\n)]{2,}/gm, '\r\n'); //remove double carriage return, as it cause issues	
    emlStr += '\n\n' + obj.body;

    return (new GlideSysAttachment()).write((new GlideRecord('sys_attachment')), sys_email_gr.subject + '.eml', 'text/plain', emlStr);
}

function generateEMLBodyAndContentType(sys_email_gr) {
    var cType = sys_email_gr.content_type.toString();
    if (cType == 'text/html' || cType == 'text/plain') {
        return {
            'contentType': 'Content-Type: ' + sys_email_gr.content_type,
            'body': sys_email_gr.body
        };
    }

    // See https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
    var contentType = 'Content-Type: text/html\n';
    var bodyStr = '\n' + sys_email_gr.body + '\n';
    bodyStr = bodyStr.replace(/<\/body>/gi, '');
    bodyStr = bodyStr.replace(/<\/html>/gi, '');

    //remove images embedded in screen (not working)
    //bodyStr = bodyStr.replace(/<img [^>]*/gmi, '');

    // Process attachments, if any.
    bodyStr += processEmailAttachmentsForEML(sys_email_gr);

    bodyStr += "</body></html>";

    return {
        'contentType': contentType,
        'body': bodyStr
    };
}


/**
 * Generates the EML content for attachments of a given email, if any.
 * @Param {GlideRecord} sys_email_gr - A sys_email GlideRecord.
 * @Param {string} boundaryStr - The boundary being used for the EML file.
 * @return {string} String containing a list of links redirecting the user to the attachments stored 
 * on servicenow. In other words, the attachments are not really part of the e-mail.
 */
function processEmailAttachmentsForEML(sys_email_gr) {
    if (!sys_email_gr) {
        throw new Error('Missing arguments.');
    }

    var attachmentList = '';
    var grAttach = new GlideRecord('sys_attachment');
    grAttach.addQuery('table_name', 'sys_email');
    grAttach.addQuery('table_sys_id', sys_email_gr.sys_id);
    grAttach.query();


    if (grAttach.hasNext()) {
        attachmentList = "<p><b><u>Attachments</u></b><ul>";

        while (grAttach.next())
            attachmentList += "<li><a href='" + gs.getProperty('glide.servlet.uri') + "sys_attachment.do?sys_id=" + grAttach.getUniqueValue() + "'>" + grAttach.file_name + "</a>";


        attachmentList += "</ul></p>";
    }

    return attachmentList;
}