Skip to content

Commit

Permalink
[MF] allow merging of :body maps to enable attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
mark.fisher committed May 21, 2014
1 parent e19d4c6 commit a7a8302
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/clojure/clojurewerkz/mailer/core.clj
Expand Up @@ -111,11 +111,21 @@
[s]
s))

(defn deep-merge-into
"Recursively merge maps applying into when there's a non-map. based on conjure.contrib/map_utils deep-merge-with"
[& maps]
(apply
(fn m [& maps]
(if (every? map? maps)
(apply merge-with m maps)
(apply into maps)))
maps))

(defn build-email
"Builds up a mail message (returned as an immutable map). Body is rendered from a given template."
([m ^String template data content-type]
(merge *message-defaults* m {:body [{:content (render template data)
:type (get-content-type content-type)}]})))
(deep-merge-into *message-defaults* m {:body [{:content (render template data)
:type (get-content-type content-type)}]})))


(defn deliver-email
Expand Down
17 changes: 16 additions & 1 deletion test/clojurewerkz/mailer/core_test.clj
Expand Up @@ -46,7 +46,7 @@
(let [email (build-email {:from "fee@bar.dom"
:to "Foo Bar <foo@bar.dom>"
:subject "Hello"}
"templates/hello.mustache" {:name "Joe"}
"templates/hello.mustache" {:name "Joe"}
:text/plain)
content (:content (first (:body email)))
type (:type (first (:body email)))]
Expand Down Expand Up @@ -104,3 +104,18 @@
;;

;; TBD

;;
;; Merging
;;

(deftest test-merged-maps
(is (= {:body [:alternative
{:type "text/plain" :content "test content"}
{:type "text/html" :content "<p>content</p>"}]
:subject "subj"
:to "person@anywhere.com"}
(deep-merge-into {:body [:alternative {:content "test content" :type "text/plain"}]
:subject "subj"
:to "person@anywhere.com"}
{:body [{:content "<p>content</p>" :type "text/html"}]}))))

0 comments on commit a7a8302

Please sign in to comment.