owainlewis / clojure-mail

A Clojure library for parsing, downloading and reading email from IMAP servers.
202 stars 54 forks source link

reading body from a message with attachments #63

Closed daonsh closed 6 years ago

daonsh commented 6 years ago

There's a known issue with the javamail class, that if a message has attachments sometimes the message body is received as com.sun.mail.util.BASE64DecoderStream@1135a5f5 And this is what I find when I do read-message using clojure-mail and take :body

A solution suggested is: https://javaee.github.io/javamail/FAQ#mainbody

I have a hard time putting that solution to work here as I don't understand exactly what the body element here is in terms of java classes and how it relates to the solution above. Or perhaps there's an easier way?

daonsh commented 6 years ago

Solution: For anyone interested in reading the body of a message with attachments, this solution should do it. For now, it takes whatever it finds (text/plain or text/html) and strips the html tags (use your own strip function) so you get the raw message text.

; complicated parsing for javamail msg to extract the body in text form. (defn get-message-body [msg] (let [body-raw-vec (:body msg) body-text (atom "")] (doall (map (fn [body-item] (let [; get as vector. if not, vectorize. body-vec (if (seq? body-item) (vec body-item) [body-item]) ] (doall (map (fn [body-elem] (let [content-type (clojure.string/upper-case (:content-type body-elem))] (if (.contains content-type "TEXT") (reset! body-text (myutil/strip-html-tags (:body body-elem)))))) body-vec)))) body-raw-vec)) @body-text))

The solution is not great-looking and is quite involved as there'r'e multiple ways in which the complex hash generated by clojure-mail can be parsed. Sometimes :body is a vec, sometimes an single object, sometimes an attachment, sometimes text/plain, sometimes text/html etc. But it works.