How do I properly capture a string in an email body?

Rochelle Rona
Tera Contributor

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.

13 REPLIES 13

Can you share the email body you are receiving.

 

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

 

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

Abhijit4
Mega Sage

Hi,

You can access only email id's from any string by using Regex pattern. Below link will help you to achieve your goal.

https://tutorial.eyehunts.com/js/javascript-extract-email-from-string-regex-example-code/#:~:text=Ex...

 

Let me know if you have any further queries/concerns.

Please mark as correct or helpful if it helps.

Thanks

Abhijit

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

I have tested this with below script, it works perfectly.

find_real_file.png

 

Output :

find_real_file.png

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

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP