Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merge old pull request

  • Loading branch information...
commit b29e2b9a0a40c2cf006f76c40a7caed62b835561 2 parents a4eb151 + 2d73be1
@owainlewis authored
View
5 README.md
@@ -29,11 +29,12 @@ In this example we'll log into a Gmail account and read messages from the inbox
;; And read the first message in our inbox
-(msg/read (first (get-inbox)))
+(msg/read-message (first (get-inbox)))
;; Get 5 messages from the spam folder
-(map msg/read (take 5 (get-spam)))
+(map #(msg/read-message %)
+ (take 5 (get-spam)))
```
View
4 project.clj
@@ -1,6 +1,6 @@
-(defproject clojure-mail "0.1.1-SNAPSHOT"
+(defproject org.clojars.petterik/clojure-mail "0.1.8"
:description "Clojure Email Library"
- :url "http://github.com/owainlewis"
+ :url "https://github.com/forward/clojure-mail"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
View
53 src/clojure_mail/core.clj
@@ -1,8 +1,8 @@
(ns clojure-mail.core
- (refer-clojure :exclude [read])
(:require [clojure-mail.store :as store]
[clojure-mail.message :as msg]
- [clojure-mail.folder :as folder])
+ [clojure-mail.folder :as folder]
+ [clojure.string :as s])
(:import [javax.mail Folder Message Flags Flags$Flag]
[javax.mail.internet InternetAddress]
[javax.mail.search FlagTerm]))
@@ -11,32 +11,21 @@
;; Very rough first draft ideas not suitable for production
;; Sending email is more easily handled by other libs
-(def ^:dynamic *settings* (ref {}))
-
-(def ^:dynamic *store* (ref nil))
+(def settings (ref {}))
(defn auth! [email pass]
(dosync
- (ref-set *settings*
+ (ref-set settings
{:email email :pass pass})))
-(defmacro with-auth [email password & body]
- `(binding [*settings* (ref {:email ~email :password ~password})]
- (do ~@body)))
-
(def gmail
{:protocol "imaps"
:server "imap.gmail.com"})
(defn gen-store []
- (let [store
- (apply store/make-store
- (cons gmail
- ((juxt :email :pass) @*settings*)))]
- (if (string? store)
- (throw (Throwable. "Invalid credentials"))
- (dosync (ref-set *store*
- store)))))
+ (let [connection (apply store/make-store (cons gmail ((juxt :email :pass) @settings)))]
+ (assert (not (string? connection)) connection)
+ connection))
(def folder-names
{:inbox "INBOX"
@@ -81,16 +70,14 @@
"Returns the number of messages in a folder"
[store folder]
(let [fd (doto (.getFolder store folder)
- (.open Folder/READ_ONLY))]
+ (.open Folder/READ_ONLY))]
(.getMessageCount fd)))
;; Public api
(defn read-all
[folder]
- (if ((complement nil?) @*store*)
- (all-messages @*store* folder)
- (throw (Throwable. "No store object exists"))))
+ (all-messages (gen-store) folder))
(defn get-inbox []
"Returns all messages from the inbox"
@@ -104,7 +91,9 @@
(defn read-message
"Reads a java mail message instance"
[message]
- (msg/read message))
+ (msg/read-message message))
+
+(defn search [query])
(def flags
{:answered "ANSWERED"
@@ -116,12 +105,20 @@
(defn unread-messages
"Find unread messages"
- [fd]
- (let [fd (doto (.getFolder (gen-store) fd) (.open Folder/READ_ONLY))
- msgs (.search fd (FlagTerm. (Flags. Flags$Flag/SEEN) false))]
- msgs))
+ [folder-name]
+ (with-open [connection (gen-store)]
+ (let [folder (doto (.getFolder connection folder-name) (.open Folder/READ_ONLY))]
+ (doall (map read-message (.search folder (FlagTerm. (Flags. Flags$Flag/SEEN) false)))))))
-(defn dump
+(defn mark-all-read
+ [folder-name]
+ (with-open [connection (gen-store)]
+ (let [folder (doto (.getFolder connection folder-name) (.open Folder/READ_WRITE))
+ messages (.search folder (FlagTerm. (Flags. Flags$Flag/SEEN) false))]
+ (doall (map #(.setFlags % (Flags. Flags$Flag/SEEN) true) messages))
+ nil)))
+
+ (defn dump
"Handy function that dumps out a batch of emails to disk"
[dir msgs]
(doseq [msg msgs]
View
15 src/clojure_mail/message.clj
@@ -1,6 +1,5 @@
(ns clojure-mail.message
- (refer-clojure :exclude [read])
- (import [javax.mail.internet MimeMultipart InternetAddress]))
+ (import [javax.mail.internet MimeMessage MimeMultipart InternetAddress]))
;; Utilities for parsing email messages
@@ -96,19 +95,21 @@
(defn- message-parts
[^javax.mail.internet.MimeMultipart msg]
- (when (multipart? msg)
+ (if (multipart? msg)
(read-multi (get-content msg))))
(defn message-body [^com.sun.mail.imap.IMAPMessage msg]
"Read all the body content from a message"
[msg]
- (let [parts (message-parts msg)]
- (into []
- (map #(hash-map (.getContentType %) (.getContent %)) parts))))
+ (into []
+ (if (multipart? msg)
+ (let [parts (message-parts msg)]
+ (map #(hash-map (.getContentType %) (.getContent %)) parts))
+ (list (hash-map (content-type msg) (.getContent msg))))))
;; Public API for working with messages
-(defn read [msg]
+(defn read-message [msg]
"Returns a workable map of the message content.
This is the ultimate goal in extracting a message
as a clojure map"
View
2  src/clojure_mail/store.clj
@@ -44,4 +44,4 @@
(try
(store protocol server user pass)
(catch javax.mail.AuthenticationFailedException e
- (format "Invalid credentials %s : %s" user pass)))))
+ (format "Invalid credentials %s : %s - %s" user pass (.getMessage e))))))
Please sign in to comment.
Something went wrong with that request. Please try again.