From 008c6a9d58a317cc70239a30ef3cfbc5afdde4c9 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 17 Jan 2018 02:02:36 +0100 Subject: [PATCH] Added support for content-id in attachments which are not type :attachment or :inline. This enables passing byte arrays as content for inline images. (#87) --- src/postal/message.clj | 2 ++ test/postal/test/message.clj | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/postal/message.clj b/src/postal/message.clj index 38c5b7f..1792a81 100644 --- a/src/postal/message.clj +++ b/src/postal/message.clj @@ -124,6 +124,8 @@ (.setDescription (:description part))))) (doto (javax.mail.internet.MimeBodyPart.) (.setContent (:content part) (:type part)) + (cond-> (:content-id part) + (.setContentID (str "<" (:content-id part) ">"))) (cond-> (:file-name part) (.setFileName (encode-filename (:file-name part)))) (cond-> (:description part) diff --git a/test/postal/test/message.clj b/test/postal/test/message.clj index b3c4432..85ec80f 100644 --- a/test/postal/test/message.clj +++ b/test/postal/test/message.clj @@ -25,8 +25,11 @@ (:use [postal.message] [clojure.test :only [run-tests deftest is]] [clojure.java.io :as io] + [clojure.string :as str] + [clojure.data.codec.base64 :as b64] [postal.date :only [make-date]]) (:import [java.util Properties UUID] + [java.lang String] [javax.mail Session Message$RecipientType] [javax.mail.internet MimeMessage InternetAddress AddressException] @@ -85,6 +88,18 @@ (is (.contains m "tempfile")) (.delete f))) +(deftest test-inline-stream + ; https://github.com/mathiasbynens/small/blob/master/png-transparent.png + (let [png-bytes (byte-array (map byte [-119 80 78 71 13 10 26 10 0 0 0 13 73 72 68 82 0 0 0 1 0 0 0 1 8 6 0 0 0 31 21 -60 -119 0 0 0 10 73 68 65 84 120 -100 99 0 1 0 0 5 0 1 13 10 45 -76 0 0 0 0 73 69 78 68 -82 66 96 -126])) + m (message->str + {:from "foo@bar.dom" + :to "baz@bar.dom" + :subject "Test" + :body [{:type "image/png" + :content png-bytes}]})] + (is (.contains (str/replace m #"[\r\n]" "") ; remove newlines to enable exact match using .contains + (String. ^bytes (b64/encode png-bytes)))))) + (deftest test-attachment (let [f1 (doto (java.io.File/createTempFile "_postal-" ".txt")) _ (doto (java.io.PrintWriter. f1)