Reading the body of an email

I am attempting to automate reading the body of an email and perform a specific action based on some keywords from the email’s body. In this case, the bot would open a SNow ticket based on some predefined keywords. I am not sure how to accomplish this. I know how to read the subject with the IMAP activity and perform an action, but trying to figure out how to read the body, skim trough the email and if certain keywords are present, go to service now and open a case.

1 Like

@machaves
The Get IMAP Mail Messages activity should return a list of MailMessage items. The Body attribute will contain the text of the body, so if your variables is called “emails” you can do emails(0).Body, emails(1).Body, etc…

To test whether the body contains certain text you can do the String Contains function. If you wanted to see if the email contained the text “Meeting” you could do emails(0).Body.Contains("Meeting") inside an If statement. If True then include the logic for whatever actions are necessary, otherwise do nothing.

2 Likes

Thank you @DanielMitchell. I must be missing something because is not executing the condition. I can see that the emails are being read.

This what I have so far.

@machaves
Can you log the body and then paste it here?

1 Like

did you try in Condition like this ?

mail.body.contains(“Password”)

1 Like

Thanks @allurai_india, I tried that.

Not sure what you mean @DanielMitchell. The body of the email I am trying to read?

@machaves
Yes, inside your For Each loop log mail.Body to the console so that we can see what is being read as the email body.

Hello, and what if you need to know if the email subject and body contains keywords (stored in a datatable)?

@E_lanotte

DT.Rows.Contains(“String”) and mail.Body.Contains(“String”) and mail.Subject.Contains(“String”)

Where String is your keyword and DT is the Datatable output of a readRange activity

I get this error now:

If: Table doesn’t have a primary key.

But I don’t use the read range because it is a CSV data table I’ve used “read CSV” is this a problem?