Reputation: 33
I am trying to parse an email getting from ring request map.
I get a body object in request map and then I use slurp to read the object. So When I do (slurp (:body req))
I am getting this output:
Date: Fri, 25 Nov 2016 09:06:30 +0630
From: [email protected]
To: localhost:8080
Cc: cccc
Subject: okay this is subject
Content-Type: multipart/alternative; boundary=T0IDt0S-7950392
--T0IDt0S-7950392
Content-Type: text/plain; charset=UTF-8
kjhdjkdshjk sivya nagar
--T0IDt0S-7950392
Content-Type: text/html; charset=UTF-8
kjhdjkdshjk sivya nagar
--T0IDt0S-7950392--
Now how to get specific details of this message like content or subject only. Any other way to parse it apart from slurp? or I have to do plain string traverse using boundary parameter?
Upvotes: 0
Views: 105
Reputation: 5766
Usually, ring middleware is used to extract the body into params
that are added to the request map. I'm not aware of any such middleware for RFC822 messages, so you might have to roll your own.
You could do the parsing by hand, but I would suggest using a library to do it for you. I imagine most Java email libraries would be capable of taking an RFC822 stream and turning it into some sort of Message
object. There's also postal, a Clojure library that wraps the JavaMail API, although I'm not sure it exposes a function for parsing messages.
Upvotes: 0
Reputation: 6509
Presumably the request contains more keys than just :body
. As you say you are looking for 'content', 'subject' etc. So use:
(keys req)
to find all the keywords that are available in the message. Next time you can use one or more of them rather than :body
.
Assuming you found that the keys were :sender
and :receiver
, and you were interested in them, you could parse the message by deconstructing their values:
(let [{:keys [sender receiver]} req]
(println "sender is" sender))
If the keys apart from :body
are not what you want then you will have to look more closely at the value of :body
. It is a Jetty input stream with a readLine
Java method that you can repeatedly call (via Clojure interop) to read in the contents. The Jetty Web Server is serving up typed Java data objects rather than Clojure data structures, thus you need to read the data using the API.
Upvotes: 1