Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

merge old pull request

  • Loading branch information...
commit b29e2b9a0a40c2cf006f76c40a7caed62b835561 2 parents a4eb151 + 2d73be1
Owain Lewis authored
5 README.md
Source Rendered
@@ -29,11 +29,12 @@ In this example we'll log into a Gmail account and read messages from the inbox
29 29
30 30 ;; And read the first message in our inbox
31 31
32   -(msg/read (first (get-inbox)))
  32 +(msg/read-message (first (get-inbox)))
33 33
34 34 ;; Get 5 messages from the spam folder
35 35
36   -(map msg/read (take 5 (get-spam)))
  36 +(map #(msg/read-message %)
  37 + (take 5 (get-spam)))
37 38
38 39 ```
39 40
4 project.clj
... ... @@ -1,6 +1,6 @@
1   -(defproject clojure-mail "0.1.1-SNAPSHOT"
  1 +(defproject org.clojars.petterik/clojure-mail "0.1.8"
2 2 :description "Clojure Email Library"
3   - :url "http://github.com/owainlewis"
  3 + :url "https://github.com/forward/clojure-mail"
4 4 :license {:name "Eclipse Public License"
5 5 :url "http://www.eclipse.org/legal/epl-v10.html"}
6 6 :dependencies [[org.clojure/clojure "1.4.0"]
53 src/clojure_mail/core.clj
... ... @@ -1,8 +1,8 @@
1 1 (ns clojure-mail.core
2   - (refer-clojure :exclude [read])
3 2 (:require [clojure-mail.store :as store]
4 3 [clojure-mail.message :as msg]
5   - [clojure-mail.folder :as folder])
  4 + [clojure-mail.folder :as folder]
  5 + [clojure.string :as s])
6 6 (:import [javax.mail Folder Message Flags Flags$Flag]
7 7 [javax.mail.internet InternetAddress]
8 8 [javax.mail.search FlagTerm]))
@@ -11,32 +11,21 @@
11 11 ;; Very rough first draft ideas not suitable for production
12 12 ;; Sending email is more easily handled by other libs
13 13
14   -(def ^:dynamic *settings* (ref {}))
15   -
16   -(def ^:dynamic *store* (ref nil))
  14 +(def settings (ref {}))
17 15
18 16 (defn auth! [email pass]
19 17 (dosync
20   - (ref-set *settings*
  18 + (ref-set settings
21 19 {:email email :pass pass})))
22 20
23   -(defmacro with-auth [email password & body]
24   - `(binding [*settings* (ref {:email ~email :password ~password})]
25   - (do ~@body)))
26   -
27 21 (def gmail
28 22 {:protocol "imaps"
29 23 :server "imap.gmail.com"})
30 24
31 25 (defn gen-store []
32   - (let [store
33   - (apply store/make-store
34   - (cons gmail
35   - ((juxt :email :pass) @*settings*)))]
36   - (if (string? store)
37   - (throw (Throwable. "Invalid credentials"))
38   - (dosync (ref-set *store*
39   - store)))))
  26 + (let [connection (apply store/make-store (cons gmail ((juxt :email :pass) @settings)))]
  27 + (assert (not (string? connection)) connection)
  28 + connection))
40 29
41 30 (def folder-names
42 31 {:inbox "INBOX"
@@ -81,16 +70,14 @@
81 70 "Returns the number of messages in a folder"
82 71 [store folder]
83 72 (let [fd (doto (.getFolder store folder)
84   - (.open Folder/READ_ONLY))]
  73 + (.open Folder/READ_ONLY))]
85 74 (.getMessageCount fd)))
86 75
87 76 ;; Public api
88 77
89 78 (defn read-all
90 79 [folder]
91   - (if ((complement nil?) @*store*)
92   - (all-messages @*store* folder)
93   - (throw (Throwable. "No store object exists"))))
  80 + (all-messages (gen-store) folder))
94 81
95 82 (defn get-inbox []
96 83 "Returns all messages from the inbox"
@@ -104,7 +91,9 @@
104 91 (defn read-message
105 92 "Reads a java mail message instance"
106 93 [message]
107   - (msg/read message))
  94 + (msg/read-message message))
  95 +
  96 +(defn search [query])
108 97
109 98 (def flags
110 99 {:answered "ANSWERED"
@@ -116,12 +105,20 @@
116 105
117 106 (defn unread-messages
118 107 "Find unread messages"
119   - [fd]
120   - (let [fd (doto (.getFolder (gen-store) fd) (.open Folder/READ_ONLY))
121   - msgs (.search fd (FlagTerm. (Flags. Flags$Flag/SEEN) false))]
122   - msgs))
  108 + [folder-name]
  109 + (with-open [connection (gen-store)]
  110 + (let [folder (doto (.getFolder connection folder-name) (.open Folder/READ_ONLY))]
  111 + (doall (map read-message (.search folder (FlagTerm. (Flags. Flags$Flag/SEEN) false)))))))
123 112
124   -(defn dump
  113 +(defn mark-all-read
  114 + [folder-name]
  115 + (with-open [connection (gen-store)]
  116 + (let [folder (doto (.getFolder connection folder-name) (.open Folder/READ_WRITE))
  117 + messages (.search folder (FlagTerm. (Flags. Flags$Flag/SEEN) false))]
  118 + (doall (map #(.setFlags % (Flags. Flags$Flag/SEEN) true) messages))
  119 + nil)))
  120 +
  121 + (defn dump
125 122 "Handy function that dumps out a batch of emails to disk"
126 123 [dir msgs]
127 124 (doseq [msg msgs]
15 src/clojure_mail/message.clj
... ... @@ -1,6 +1,5 @@
1 1 (ns clojure-mail.message
2   - (refer-clojure :exclude [read])
3   - (import [javax.mail.internet MimeMultipart InternetAddress]))
  2 + (import [javax.mail.internet MimeMessage MimeMultipart InternetAddress]))
4 3
5 4 ;; Utilities for parsing email messages
6 5
@@ -96,19 +95,21 @@
96 95
97 96 (defn- message-parts
98 97 [^javax.mail.internet.MimeMultipart msg]
99   - (when (multipart? msg)
  98 + (if (multipart? msg)
100 99 (read-multi (get-content msg))))
101 100
102 101 (defn message-body [^com.sun.mail.imap.IMAPMessage msg]
103 102 "Read all the body content from a message"
104 103 [msg]
105   - (let [parts (message-parts msg)]
106   - (into []
107   - (map #(hash-map (.getContentType %) (.getContent %)) parts))))
  104 + (into []
  105 + (if (multipart? msg)
  106 + (let [parts (message-parts msg)]
  107 + (map #(hash-map (.getContentType %) (.getContent %)) parts))
  108 + (list (hash-map (content-type msg) (.getContent msg))))))
108 109
109 110 ;; Public API for working with messages
110 111
111   -(defn read [msg]
  112 +(defn read-message [msg]
112 113 "Returns a workable map of the message content.
113 114 This is the ultimate goal in extracting a message
114 115 as a clojure map"
2  src/clojure_mail/store.clj
@@ -44,4 +44,4 @@
44 44 (try
45 45 (store protocol server user pass)
46 46 (catch javax.mail.AuthenticationFailedException e
47   - (format "Invalid credentials %s : %s" user pass)))))
  47 + (format "Invalid credentials %s : %s - %s" user pass (.getMessage e))))))

0 comments on commit b29e2b9

Please sign in to comment.
Something went wrong with that request. Please try again.