How do I properly capture a string in an email body?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 10:20 PM
Hi,
In an inbound email, the email template contains "Process the request from test@test.com". I need to capture the email.
I have the code "var reqEmail = email.body.process_the_request_from" and have it set to the short description but it's returning undefined.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 12:34 AM
Can you share the email body you are receiving.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 12:45 AM
It differs from template to template, but basically this is the email body:
Hi Team,
Process the request from test@test.com
Name: test
Position: test
Regards,
TeamMember

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 02:39 AM
Hi,
So for this template you can use below script and do changes for other templates as per your needs.
/*Template
Hi Team,
Process the request from test@test.com
Name: test
Position: test
Regards,
TeamMember
*/
var template = email.body_text.toString();//Convert the body text into string format.
var st1 = template.indexOf('Process');//get the index value of line which have the email.
// Getting the line "Process the request from test@test.com"
var emailID = template.slice(st1,st1+38);
var splitReqEmail = emailID.split('from');
var reqEmail = splitReqEmail[1];//test@test.com
I would suggest to have an predefined subject line which have this email address value in it instead of mentioning in the body of email.
Through subject line it will be much easier approach.
Mark this as Helpful/Correct if this helps you in anyways.
Regards,
Sourbh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 12:22 AM
Hi,
You can access only email id's from any string by using Regex pattern. Below link will help you to achieve your goal.
Let me know if you have any further queries/concerns.
Please mark as correct or helpful if it helps.
Thanks
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 01:08 AM
I have tested this with below script, it works perfectly.
Output :
Regex pattern: text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
Let me know if you have any further queries/concerns.
Please mark as correct or helpful if it helps.
Thanks
Abhijit
Regards,
Abhijit
ServiceNow MVP