From cf47794df3788be0f1ae7a7a37230c56de855c1b Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Sat, 5 Dec 2015 11:32:31 +0100 Subject: [PATCH] Start with the email parser --- src/mailhead/parser.clj | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/mailhead/parser.clj diff --git a/src/mailhead/parser.clj b/src/mailhead/parser.clj new file mode 100644 index 0000000..7baff6d --- /dev/null +++ b/src/mailhead/parser.clj @@ -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))))