Start with the email parser

This commit is contained in:
Aaron Fischer 2015-12-05 11:32:31 +01:00
parent edbc2d432d
commit cf47794df3

29
src/mailhead/parser.clj Normal file
View file

@ -0,0 +1,29 @@
(ns mailhead.parser
(:require
[clojure.reflect :as r])
(:use [clojure.contrib.java-utils :only [as-properties]]
[clojure.java.io :only [input-stream file]]
[clojure.pprint :only [print-table]])
(:import
(javax.mail Session)
(javax.mail.internet MimeMessage)
(javax.mail.internet InternetHeaders)))
(def session
(Session/getDefaultInstance
(as-properties [["mail.store.protocol" "imaps"]])))
(defn all-headers [message]
(let [headers (enumeration-seq (:allHeaders (bean message)))]
(map (fn [header]
(assoc {} (.getName header) (.getValue header)))
headers)))
(defn )
(defn parse-from-string [email-string]
(let [email-stream (java.io.ByteArrayInputStream. (.getBytes email-string))]
(all-headers (MimeMessage. session email-stream))))
(defn parse-from-file [filename]
(all-headers (MimeMessage. session (input-stream filename))))