Question

assystETM - importing email need string from mailbody

  • 20 December 2022
  • 9 replies
  • 235 views

Badge

Hi,

we are reading mails from monitoring-tools in order to create new tickets.
The CI/itemA of the new ticket is found in the mailbody.

How can we extract the mailbody text ? I need this text ‘oem-12.dcc.562.com
Example of text in the mailbody:
Subject: EM Event.d.d9. Please check log for details.

Host=oem-12.dcc.562.com

Target type=blbaopdf

..

Thanks for help
Marion


9 replies

Userlevel 4
Badge +12

We use the ETM to read mailboxes with the following code to pull the body.

if(!inbound['text/html']){
inbound['text/plain']
}else{
inbound['text/html']
}

 

If the format of the inbound always remains the same, you could use a regex expressions to search the body for matches. The regex below has been substituted to work with your example string, but our looks something like….

var bodyText = String(inbound.emailBody)
var regEx = /[a-z]{3}[-][0-9]{2}[.][a-z]{3}[.][0-9]{3}[.][com]{3}/;
var matches = regEx.exec(bodyText);

if(matches){
// Do something interesting
} else {
// Do nothing
}

…if not then maybe use indexOf to find “Host=” and pull x number of characters that follow. 

Badge

Hello Steve,

do you now a solution with your code to get the name after the Host= as output?

So if we got Host=oem-12.dcc.562.com in our mail body, the output should be oem-12.dcc.562.com.

Our problem is that the names can be structured differently.

Examples:

 - Host=oem-12.dcc.562.com

 - Host=HSTG-20887.TG

 - Host=Kernel.On-5489

The only thing we have in common is that we don't have spaces in our Hostnames.

Kind regards

Jo Schmitz

Userlevel 2
Badge +9

I recommend using https://regex101.com/ to experiment with Regular Expressions. Use ‘the ECMAScript’ option to get the same behavior as ETM.

 you can use 

var regEx = Host=([^\s]+)

Which says ‘match Host= anything up to the first whitespace character, and “capture” the bit (“group”) after the =

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions & https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Backreferences

The second page contains a handy place to play with the JavaScript/

 

Badge

Hello Paul,

thanks for your request.

I am using this code (build out of your answers):

if(!inbound['text/html']){
inbound['text/plain']
}else{
inbound['text/html']
}

var regEx = /Host=([^\s]+)/;
var matches = regEx.exec(inbound);

if(matches){
"True"
} else {
"False"
}

The variable “inbound” contains at two points:

 - <p class="MsoNormal"><code><span style="font-size:10.0pt">Host=oem-12.dcc.562.com</span></code><o:p></o:p></p>

 - Host=oem-12.dcc.562.com

Unfortunately the result shows “False”.

If I try your code here https://regex101.com/ everthing works fine.

 

I also don’t understand how to get the result “ oem-12.dcc.562.com ” as output

Kind regards

Jo Schmitz

Userlevel 2
Badge +9

inbound contains 2 objects - the html part and the plain text part (assuming the delivered email also contains both of these).

If you know that your email will always contain the plain text part then we can just work with that:

var content= inbound[‘text/plain’]

RegExps need to start with a / - sorry I missed that

var regEx = /Host=([^\s]+)/

You use the match() function of a string to apply the regular expression to a string - it returns the text that matches and the capture group

var content = "Subject: EM Event.d.d9. Please check log for details."
+"\Host=oem-12.dcc.562.com"
+"\nTarget type=blbaopdf";

var re = /Host=([^\s]+)/;

var matches = content.match(re);

//if we have a match then the second element in the aray is the 1st capture group
var host = matches && matches[1]

console.log(host)

Try that in the JS “Test It” tool at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Backreferences or https://jsfiddle.net/

Badge

Hello Paul,

many thanks for the support.

This solution (build by your tips) works perfektly:

var content = inbound['text/plain']
var re = /Host=([^\s]+)/;
var matches = content.match(re);
var host = matches && matches[1]
host

Many many thanks.

Kind regards

Jo Schmitz

Badge

One more Question:

I try to set the result to upper cases.

host.toUpperCase()

Error: ECMAException - TypeError: null has no such function "toUpperCase"

Do you know another function?

 

Could’t you also tell me wich language the ETM exactly uses?

It’s not the JAVA I know.

Kind regards

Jo Schmitz

Userlevel 2
Badge +9

It’s javascript/ECMA Script. What I suspect is happening is that you have an email without a Host entry. In that case matches will be null and so will host.

Calling a method (e.g. toUpperCase) on null will result is that error. 

If you change your last line to 

host && host.toUpperCase() 

then you won’t get the error. See https://developer.mozilla.org/en-US/docs/Glossary/Truthy for an explanation of this use of logical and (which I also used on the var host = … line).

Userlevel 2
Badge +8

I usually just do some string magic with javascript to get that from the email body, when the email body will be the same for every email processed by the channel.


For example, I have one where i need to set the item for the ticket based on data from the body of the email below, between the “Bookings Subject:” text and the “Bookings Appointment Detail:” text.
 

To do so, I first create a variable to “clean” the body of the email by removing all html tags, etc.
 

Then I do the following, to find the start and end of the data I need. varFindItemStart finds the index of the text before the data I want, “Bookings Subject:” varFindItemEnd finds the the index of the text at the end of the data I want, “Bookings Appointment Detail.”
 

Now that I have these two numbers, I create another Variable to parse the text between those two index values using substring. 
 


Now that I have the value I need from between “Bookings Subject:” text and the “Bookings Appointment Detail:”, I created a lookup table of the possible values and the corresponding Item Shortcode.

 

 

Then I create a Variable Lookup using this lookup table, where I pass the value I created in the varGetItem variable to the lookup, which returns the Item SC for that lookup value. I then use the new variable, varFindItem as the Shortcode for the Item.

 

 

It is very convoluted and there’s probably a much easier way to do it, but it works for me and it’s easy to use again, you just have to swap out the “start text” and “end text” values and it will pull everything in between. 

So in your case, you would set the varFindItemStart to:


variables.varCleanDesc.indexOf('Host=')+5
 

and the varFindItemEnd to:
 

variables.varCleanDesc.indexOf('Target type=')

so then in your varGetItem variable, you would set it to:

variables.varCleanDesc.substring(variables.varFindItemStart,variables.varFindItemEnd).trim();  

Which should give you the value of oem-12.dcc.562.com for the varGetItem variable.

Reply