logo

NJP

Speed up email delivery by disabling notifications for users with bounced emails

Import · Oct 05, 2017 · article

return _retrieveBouncedBackEmail(queryToMatch, maxToFindPerEmail, limitquery);

function _retrieveBouncedBackEmail(queryToMatch, maxToFindPerEmail, limitquery) { // Set max emails retrieved from each bounced email parsed - default 10 maxToFindPerEmail = maxToFindPerEmail || 10; // Limit parsing more than limitquery emails - default 100

limitquery = limitquery ? limitquery : 100;

// Set the query that matches the bounced emails - default: "type=received-ignoredsys_created_onONThis week

queryToMatch = queryToMatch || "type=received-ignoredsys_created_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()";

// Set the response message and the query to perform var vmessages = ["Query limit set " + limitquery, "Searching on sys_email", "Query: " + queryToMatch, "Threshold: " + maxToFindPerEmail], b = new GlideRecord("sys_email"); b.addEncodedQuery(queryToMatch); b.setLimit(limitquery); b.query(); vmessages.push(" sys_email records returned: " + b.getRowCount()); // Loop on the matched emails and assume the body contains the list of emails bounced back for (var lemails = []; b.next();) { var a = b.body_text, // Ignores the text after "Received:" on the body_text as it contains the emails from the original message // that you do not want to parse. You want to retrieve only the ones related to the bounced email g = a.indexOf("Received:");

0 < g && (a = a.substr(0, g));

a = (a = extractEmails(a)) ? uniq(a) : [];

// It would report on email bound back on which we could not extract an email // so you need to inspected it, to validate why it did not retrieve an email 0 == a.length && vmessages.push("Email we could not extract an email addresses: " +

b.sys_id + " - count: " + a.length);

// If the email contains too many emails, it will add a message // so you need to inspected it, to validate why there were so many emails a.length > maxToFindPerEmail && vmessages.push("Email ignored by threshold: " + b.sys_id + " - count: " + a.length); (0 < a.length && a.length) <= maxToFindPerEmail && (lemails = uniq(lemails.concat(a)))

}

// returns an array with the list of arrays: // [0] array of message [1] array of emails return [vmessages, lemails]

};

// Generate an array of emails found on the text provided function extractEmails(text) { return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);

}

// Sort and remove non-unique strings on the array function uniq(a) { return a.sort().filter(function(item, pos, ary) { return !pos || item != ary[pos - 1]; }) }

}

Here is the script for setUserNotificationbyEmail:

// Set the User 'notification' to Enable/Disabled for the matching email addresses// vemailarray is the array of emails to disable// setEnable is either "enabled" or "disabled". Disabled is the defaul// limitquery is the max number of users to set. It has a limitquery of 100//

function setUserNotificationbyEmail(vemailarray, setEnable, limitquery) {

      // It will only query the users that need to be enabled or disabled, and ignore the ones already set.       var c = (setEnable = /enable|enabled|true|1|yes|on$/i.test(setEnable)) ? "notification=1emailIN" + vemailarray.join(",") : "notification=2emailIN" + vemailarray.join(","),               b = new GlideRecord("sys_user");       limitquery = limitquery ? limitquery : 100;       b.addEncodedQuery(c);       b.setLimit(limitquery);       b.query();       // create the message to provide back       c = ['Query Limit set ' + limitquery, 'Records found: ' + b.getRowCount(), 'Query executed: ' + c];       // Here the users are being set       for (; b.next();) c.push((setEnable ? "Enabled " : "Disabled ") + "notification: " + b.sys_id + " - user: " + b.user_name), b.notification = setEnable ? 2 : 1, b.update();       return c

};

For an example, here is a bounced email found on an instance:

image

Executing:

// set the qualification to match your bounced emails.  

var result = retrieveBouncedBackEmail("sys_idSTARTSWITHc3a63a97dbc14bc0d975f1c41d9619b7", 45, 1000);

gs.print("\n Results:\n" + result[0].join("\n") + "\n\nFinal list of emails " + result[1].length + "\n\n\n" + result[1].join("\n"));

RESULT:

*** Script: Results:Query limit set 1000Searching on sys_emailQuery: sys_idSTARTSWITHc3a63a97dbc14bc0d975f1c41d9619b7Threshold: 45 sys_email records returned: 1Final list of emails 3andrea.sisco@abc.com.nettest-bb.aa@abc.com.net

test.aaa@abc.com.net

To disable the users, you can execute the following:

// create an array of emails, for the users you want to disable.

var todisablelist = ["andrea.sisco@abc.com.net","test-bb.aa@abc.com.net","test.aaa@abc.com.net"]

// To disable the User Notification uncomment the next line

gs.print( '\n\nSetting user disabled:\n\n' + setUserNotificationbyEmail(todisablelist,'Disabled').join('\n'));

RESULT:

*** Script:

Setting user disabled:

Query Limit set 100Records found: 1

Query executed: notification=2emailINandrea.sisco@abc.com.net,test-bb.aa@abc.com.net,test.aaa@abc.com.net

Disabled notification: 8d256345dbe983002fd876231f96196e - user: andrea.sisco

Or you can use them together as follow:

// set the qualification to match your bounced emails.  

var result = retrieveBouncedBackEmail("sys_idSTARTSWITHc3a63a97dbc14bc0d975f1c41d9619b7", 45, 1000);

gs.print("\n Results:\n" + result[0].join("\n") + "\n\nFinal list of emails " + result[1].length + "\n\n\n" + result[1].join("\n"));

// result[1] hold the array of emails, for the users you want

// To disable after you validate the User Notification result[1] contains an array of the emails

gs.print( '\n\nSetting user disabled:\n\n' +

    setUserNotificationbyEmail(todisablelist,'Disabled').join('\n'));

RESULT:

*** Script:Results:Query limit set 1000Searching on sys_emailQuery: sys_idSTARTSWITHc3a63a97dbc14bc0d975f1c41d9619b7Threshold: 45sys_email records returned: 1Final list of emails 3

andrea.sisco@abc.com.net

test-bb.aa@abc.com.net

test.aaa@abc.com.netSetting user disabled:Query Limit set 100Records found: 1

Query executed: notification=2emailINandrea.sisco@abc.com.net,test-bb.aa@abc.com.net,test.aaa@abc.com.net

Disabled notification: 8d256345dbe983002fd876231f96196e - user: andrea.sisco

Please note the user notification value does not affect the "active" or "lock" status on the account. It will only affect the notifications.

I hope these actions empower the administrator to set a user account's notifications to Disable. This would dramatically reduce the amount of emails being bounced back.

Archive junk emails

ServiceNow can archive and eventually destroy email messages that you no longer need, in the case Junk emails or if your Email table is excessively large. This is especially important if you depend on emails as very large tables tend to degrade over time and delay upgrades.

As per performance, we are looking for email retention rules: "Emails - Ignored and over 90 days old". This rule archives email message records that were created more than 90 days prior to the current date and are of type received-ignored or sent-ignored.

image

Once the emails are archived, then they stay on the archive for a year before being destroyed.

image

Now, it is time to put this all in place and allow your users to unsubscribe. For those which the notifications bounce back, then set the sys_user 'notification' to Disabled. Finally, for those emails ending up on your instance Junk, archive and destroy them. If you keep doing this, your email delivery will be reliable and delays will be a thing of the past.

Here are some useful resources for more information :

Labels:

View original source

https://www.servicenow.com/community/developer-blog/speed-up-email-delivery-by-disabling-notifications-for-users/ba-p/2291260