Export email from sys_email to sys_attachment in ".eml" format

Rafael Batistot
Tera Sage

Hi Team, 

Is it possible? Export email from sys_email to sys_attachment in ".eml" format?

Thank's in advanced

1 ACCEPTED SOLUTION

Hi @Rafael Batistot ,

 

try this

var current = new GlideRecord("sys_email");
if (current.get("dd2adb63c3916a10051cb132b401318c")) {  // get the sys_email record that you want to convert as an attachment
    gs.info(current.getDisplayValue());
}
//content of the mail 
//I have used template literals `` for this to work enable es6 on you script or use string concatinaiton(+)
var content = `${current.headers}
------=_Part_103_1432489236.1740299923118
Content-Type: multipart/alternative; 
	boundary="----=_Part_104_1164394642.1740299923118"

------=_Part_104_1164394642.1740299923118
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=UTF-8
${current.body}
------=_Part_104_1164394642.1740299923118--`;

var attachment = new GlideSysAttachment();

//set up inputs
var rec = new GlideRecord('incident');
rec.get('197ed7e7c3916a10051cb132b40131fb');
var fileName = 'testEmail.eml';
var contentType = '	message/rfc822';


var agr = attachment.write(rec, fileName, contentType, content) // this attaches the email to target record in this case it is 

I have taken example sys_email and incident record
I have added few comments too

adjust this as per your requirement

 

result

 

ChaitanyaILCR_0-1747105042916.png

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

3 REPLIES 3

Chaitanya ILCR
Kilo Patron

Hi @Rafael Batistot ,

 

I have tried this 

use GlideSysAttachment and build proper content from sys_email 

you should be able to create .eml files

 

var current = new GlideRecord("sys_email");
if (current.get("74f36a58c3ad2e10051cb132b401310b")){
    gs.info(current.getDisplayValue());
}

var attachment = new GlideSysAttachment();

//set up inputs
var rec = new GlideRecord('incident');
rec.get('c3e0778cc3146e10051cb132b40131b2');
var fileName = 'test.eml';
var contentType = '	message/rfc822';
var content = current.body;

var agr = attachment.write(rec, fileName, contentType, content);

gs.info('The attachment sys_id is: ' + agr);

 

result

ChaitanyaILCR_0-1747072413367.png

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

Hi Chaitanya, 

Thank you for your support. It works but in parts. The body coming but the header not 

RafaelBatistot_0-1747075092798.png

 



Hi @Rafael Batistot ,

 

try this

var current = new GlideRecord("sys_email");
if (current.get("dd2adb63c3916a10051cb132b401318c")) {  // get the sys_email record that you want to convert as an attachment
    gs.info(current.getDisplayValue());
}
//content of the mail 
//I have used template literals `` for this to work enable es6 on you script or use string concatinaiton(+)
var content = `${current.headers}
------=_Part_103_1432489236.1740299923118
Content-Type: multipart/alternative; 
	boundary="----=_Part_104_1164394642.1740299923118"

------=_Part_104_1164394642.1740299923118
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=UTF-8
${current.body}
------=_Part_104_1164394642.1740299923118--`;

var attachment = new GlideSysAttachment();

//set up inputs
var rec = new GlideRecord('incident');
rec.get('197ed7e7c3916a10051cb132b40131fb');
var fileName = 'testEmail.eml';
var contentType = '	message/rfc822';


var agr = attachment.write(rec, fileName, contentType, content) // this attaches the email to target record in this case it is 

I have taken example sys_email and incident record
I have added few comments too

adjust this as per your requirement

 

result

 

ChaitanyaILCR_0-1747105042916.png

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya