I am trying to extract the email address from the automatic emails saying my email was not delivered.
In order to do that, I run a regex on the string displaying the email address.
Sadly, the item path changes in some of the emails and therefore an error occurs.
var text = $item("0").$node["Webhook"].json["body"]["latest_message"]["preview"]; // The string containing
var re = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi;; // The actual regex
var email = text.match(re)
return [{
json: { email }
}]
Most of the time, the end of the path changes to [“message”][“preview”] or [“last_message”][“preview”].
If I enter a path that doesn’t exist, the function returns an error.
Pretty sure there is a simple solution to my problem, sadly I have not been able to find it by myself.
It is not, as there are other email adresses in the body, such as the the sender of the automatic email (server) and some other email adresses relative to the mailing client I use.
In the end, the easier solution would certainly be to learn the possible paths this string will have, then scan them all to know which one exists, and then run the regex on it.
Like if item1 exists, then run regex on item1, if not scan if item> exists… and so on.
You should be able to have multiple Regex possibilities with the use of the pipe in the regex.
As you have noticed the use of different fields will result in errors. You can ignore the errors and just have some checks after each other.
Or you will have to come up with a check on the whole body so you do not have to deal with errors.
Hope that makes sense.
If you would still like some help. Please provide some examples of the output you are expecting. Maybe someone will have a great idea to help you out.