Randall Bohn
Randall Bohn

Reputation: 2657

How do I access the message body from the Kynetx email endpoint?

I want to extract a URL from an inbound email message and then http:get() the URL. How can I access the message body?

select when mail received from "(.*)@example.com" setting user
pre { /* extract first URL from message */ }
http:get(URL);

So what goes in the PRE block, given the following email message:

From: Example User <[email protected]>
To: x202 Endpoint <[email protected]>
Subject: An interesting URL

http://www.example.net

Upvotes: 4

Views: 63

Answers (1)

TelegramSam
TelegramSam

Reputation: 2790

You use the email:parts() method to extract the portions of the email. In a multipart email, you will have both text/html and text/plain parts.

To access the email, you first extract the email (in RFC822 form) from the msg event param, like so:

envelope = event:param("msg");

Then, you can use the parts method to extract a portion. This code example extracts the plain text portion of the email:

textportion = email:parts(envelope,"text/plain").pick("$..text/plain");

Calling email:parts(envelope) without passing a mime filter will return a struct with all the parts of the email.

Once you have the body, you can use textportion.extract(re//) to extract information from the email body.

Upvotes: 3

Related Questions