4

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 <user@example.com>
To: x202 Endpoint <a101x202@kynetxapps.net>
Subject: An interesting URL

http://www.example.net
Randall Bohn
  • 2,597
  • 2
  • 16
  • 20

1 Answers1

3

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.

TelegramSam
  • 2,770
  • 1
  • 17
  • 22