Send mail with mail API including attachment

Tommy Jensen
Giga Guru

I would like to send an email via REST API from an external application

https://developer.servicenow.com/app.do#!/rest_api_doc?v=orlando&id=r_email-api-post

 

and I want to include files but the documentation does not specify how to do that. Does anybody know how to?

6 REPLIES 6

Hello @Tommy Jensen , have you got any confirmation on this ? I have same requirement need to send attachment with email in a single go using one api .

 

 

Thanks 

 

Kumar

Hi @kumarsatyam 

 

The below code could be helpful if you want to send a html file as an attachment :

 

var subject = '';
  
var mailTo = '';
  
var attachmentfileName = '';

var mailHTMLBody = '';
  
var attachmenHTMLContent = '';

//Inserting record in sys_email table
var mailGr = new GlideRecord('sys_email');
  
mailGr.initialize();
  
mailGr.type = 'send-ignored';
  
mailGr.notification_type = 'SMTP';
  
mailGr.subject = subject;
  
mailGr.recipients = mailTo.toString();
  
mailGr.body = mailHTMLBody;
  
mailGr.content_type = 'multipart/mixed';
  
mailGr.insert();

var htmlData = attachmenHTMLContent.toString() + "\r\n";
  
var content = '';

//Inserting in sys_attachement table of mailgr record.
var sa = new GlideSysAttachment();
  
sa.write(mailGr, attachmentfileName, 'application/html', htmlData);

var attachmentget = new GlideSysAttachment();
  
var agr = attachmentget.getAttachments('sys_email', mailGr.sys_id);
  
if (agr.next()) {
    
  	var sysEmailAttachment = new GlideRecord('sys_email_attachment');
  
    sysEmailAttachment.initialize();
  
    sysEmailAttachment.attachment = agr.sys_id;
  
    sysEmailAttachment.file_name = agr.file_name.toString();
  
    sysEmailAttachment.source = 'notification';
  
    sysEmailAttachment.content_disposition = 'attachment';
  
    sysEmailAttachment.email = mailGr.sys_id;
  
    sysEmailAttachment.insert();
}

mailGr.type = 'send-ready';
  
mailGr.update();

 

Please like and mark the answer as correct and helpful if it helps.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.