Inbound Email Actions vs Regex to extract email addresses
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2015 02:59 PM
Hi folks
I was wondering if I could get your expertise on a JavaScript Regex scripting on Inbound Action Routing this time. the action is to extract all email addresses recognized by a Regex parse,
below is the code,
The issue is the first line 'emailAddresses =email.body_html.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);' does not appear to return an array, which I thought it would;
Debugging emailAddresses.length=undefined, but emailAddresses returns all email addresses.
Here is the code in full
var emailAddresses = email.body_html.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
logger.logInfo('script:emailAddress=' + emailAddresses);
// fetch email addresses
var iLoop = 0;
if (emailAddresses != '') {
do {
logger.logInfo("emailAddresses[0].indexOf(',')=" + emailAddresses[0].indexOf(','));
logger.logInfo("emailAddresses[0]=" + emailAddresses[0]);
var emailAddress='';
if (emailAddresses.indexOf(',')==-1)
emailAddress = emailAddresses;
else {
var j = emailAddresses.indexOf(',');
emailAddress = emailAddresses(0,j);
emailAddresses = emailAddresses.slice(j+1);
};
logger.logInfo('Recipient ' + (iLoop+1) + ':' + emailAddress);
iLoop +=1;
} while (emailAddresses.indexOf(',') != -1 && iLoop < 10)
}
Any advise would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2015 04:51 AM
Hi George,
I took your Regex code and run it over my own test instance and it works, see below:
var gr = new GlideRecord('sys_email');
gr.addNotNullQuery();
gr.setLimit(1);
gr.query();
while (gr.next()){
gs.print('Here starts the header \n');
gs.print(gr.headers);
gs.print('Here ends the header \n');
var emailAddresses = gr.headers.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
gs.print('Here are the ' + emailAddresses.length + ' email addresses we found: \n');
gs.print(emailAddresses + '\n');
}
It returned:
[0:00:00.028] Script completed in scope global: script
*** Script: Here starts the header
*** Script: Date: Fri, 23 Oct 2015 13:10:13 -0700 (PDT) From: IT Service Desk <empspanaite@service-now.com> Reply-To: IT Service Desk <empspanaite@service-now.com> To: sergiupsnow@gmail.com Message-ID: <16911286.3.1445631013629.JavaMail.p16002@app48014.ams3.service-now.com> Subject: Change Request CHG0030002 comments added -- This is for testing MIME-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit X-ServiceNow-Source: Notification-69fd239d4a36231200b9301f1c5c0553 X-ServiceNow-SysEmail-Version: 2 Precedence: bulk Auto-Submitted: auto-generated X-ServiceNow-Generated: true
*** Script: Here ends the header
*** Script: Here are the 4 email addresses we found:
*** Script: empspanaite@service-now.com,empspanaite@service-now.com,sergiupsnow@gmail.com,16911286.3.1445631013629.JavaMail.p16002@app48014.ams3.service-now.com
I am not sure how "emailAddresses" returns all email addresses, but "emailAddresses.length" is undefined. That is a bit strange, as you can see above in my case it returns the number of email addresses found.
Can you take my example and run it via background scripts and see if it works in your instance?
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2016 09:00 PM
Hi Sergiu
Thanks for the analysis. Yep I have run the code and it returned emailAddresses.length properly with the below
*** Script: Here starts the header
*** Script: From: CorpTech Service Desk <servicedesk@xxxx>
Reply-To: CorpTech Service Desk <servicedesk@xxxxx>
To: xxx
Subject: Testing TLS
Precedence: bulk
*** Script: Here ends the header
*** Script: Here are the 3 email addresses we found:
*** Script: xxx@xxx.com,xxx@xxx.com,xxx@xxx.com
The script I ended up running in a new Inbound Action Script which fetch every email address found in email body content, and check against sys_user table to see if there is a match; if so, update the email status to Invalid email
var iAction = new QSS_UserEmailUtil(); // which is a script include library
var emailAddresses = email.body_html.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
logger.logInfo('script:emailAddress=' + emailAddresses);
// fetch email addresses
if (emailAddresses != '') {
logger.logInfo("emailAddresses[0]=" + emailAddresses[0]); // write the first address to sys_log
for (i=0; i< emailAddresses.length; i++) {
logger.logInfo('Recipient ' + (i+1) + ':' + emailAddresses[i]); // iterate through each email address
var user = new QSSLibrary_GetUserByEmail().getUserByEmail(emailAddresses[i]);
if (typeof user != "undefined") {
if (iAction.updateUserEmailStatus(user)) { // if update successful
logger.logInfo("script:User: '" + user.user_name + "' Email Status Updated to Invalid Email (1)");
//stop the loop
break;
}
} else
logger.logInfo('script:User Email' + emailAddresses[i] + ' was not found');
}
}
logger.logInfo('script:Email Action Script (Test) Undeliverable triggered');
The script seems to work as expected though not understand why it was generating undefined on the emailAddresses array
Thanks for the feedback.
Kind regards,