Vedhavrath_Kond
Tera Guru

This Article explains about GlideEmailOutbound API which I have bumped into recently. ServiceNow email notifications can be triggered in several ways such as through workflows, record actions, events, and flow designer. One exciting way of triggering a notification is by using GlideEmailOutbound() API.

 

GlideEmailOutbound class implements the email object for scoped applications. It is identical to the email global object available in mail scripts. One can use the GlideEmailOutbound class to send an email without creating a notification record in the Instance. Some of the useful methods of GlideEmailOutbound class are mentioned below.

 

setReplyTo(String address)

This method sets the reply-to address for the email notification. The input type is of string.

 

setFrom(string address)

This method sets the from address for the email notification. The input type is a string.

 

addAddress(Sring type,String address)

This method adds the address to either the cc or bcc list of the email. The input type is a string.

 

setBody(String body Text)

This method adds the body of the email. The input type is a string.

 

setSubject(String subject)

This method sets the subject of the email. The input type is a string.

 

save

This method triggers the notification.

 

Example:

var mail = new GlideEmailOutbound();

mail.setReplyTo('agent@abc.com');

mail.setSubject('Testing outbound email'); 

mail.addAddress('cc', 'admin@example.com'); 

mail.setBody('Hello world!');

mail.save();

find_real_file.png

 

However, GlideEmailOutbound() class lacks the ability to send attachments or add templates to the outbound email. Because of this ServiceNow recommends sending notifications by events and altering the properties of the email using mail scripts.

 

Please refer to ServiceNow Docs for more details

Comments
dev115415
Tera Expert

Hello when i try to use  the following code in my script include it does not work.

 

// Create a new instance of the GlideEmailOutbound API
var email = new GlideEmailOutbound();

// Define the email parameters
email.setSubject('Notification');
email.setBody('Values: ' + values.join(', '));
email.setFrom('your_email_address');
email.addTo('recipient_email_address');

// Send the email
email.send();

dev115415
Tera Expert

hell @Vedhavrath_Kond this is not working for me. I tried your code snippet in servicenow script include.

Vedhavrath_Kond
Tera Guru

Hi @dev115415 , Is your script include scope specific or work on all scopes? GlideEmailOutbound() is a scoped API, make sure your script include is accessible for any scope

dev115415
Tera Expert

hi @Vedhavrath_Kond I have created a scoped application and there I am trying to use it.

I have one more question where will we specify the server, port password of the sender?

Qyc
Tera Contributor

 Hello @Vedhavrath_Kond 

I have the same doubt that how to set the recipient('to'?) instead of 'cc' or 'bcc'?

Vedhavrath_Kond
Tera Guru

@Qyc Please use setReplyTo() method, it is mentioned in the article

Venkata Ramesh
Tera Contributor

How can we add To address with this API?

Mahesh23
Mega Sage

Hello,

 

You can use addRecipient() to set To address.

 

var mail = new GlideEmailOutbound();

mail.setReplyTo('agent@abc.com');

mail.setSubject('Testing outbound email');

mail.addAddress('cc', 'admin@example.com');

mail.setBody('Hello world!');

mail.addRecipient('');

mail.save();

 

HAMDI Oussema
Tera Contributor

It needs to be under global application

Paul Sisson
Tera Expert

JUST A QUICK ADDITION TO THIS HELPFUL POST:

 

var email = new GlideEmailOutbound()

var email = new GlideEmailOutbound()   <-- initialize the email

 

email.setRecipients('guy@email.com')

email.setRecipients('guy@email.com') <-- add the recipients (not included in original post)

 

email.setBody("<h1 style='color: green;'>Style me!</h1>")

email.setBody("<h1 style='color: green;'>Style me!</h1>") <-- You can use and HTML string here. This is very important for those of us that don't want to send really ugly emails.

Version history
Last update:
‎07-04-2022 09:05 AM
Updated by: