Liking cljdoc? Tell your friends :D

Clojure-mail

This is a fork of the canonical clojure-mail repo.

You can require it with:

[org.clojars.mathias/clojure-mail "0.2.0"]

A clojure library for parsing, downloading and reading email from Gmail servers.

Possible uses for this library include machine learning corpus generation and command line mail clients.

Setup

(:require [clojure-mail.core :refer :all])

Authentication

First create a mail store session like this:

(def store (gmail-store "username@gmail.com" "password"))

Then use store when you call other functions, like this:

(inbox store 5)

;=> ...

You can also use the with-store macro to wrap your code with an open store, like this:

(with-store (gmail-store "username@gmail.com" "password")
  (inbox 5))
;=> ...

Notice how we didn't have to pass store when we used the macro?

Reading email messages

Let's fetch the last 3 messages from our Gmail inbox


(def inbox-messages (inbox "username@gmail.com" "password" 3))

;; Lets fetch the subject of the latest message

(:subject (first inbox-messages))

;; => "Booking confirmed (MLC35TJ4): Table for 2 at The Potted Pig 22 March 2014 - at 13:30"

;; The following keys are available on an email message

(keys (first inbox-messages))

(:subject :from :date-recieved :to :multipart? :content-type :sender :date-sent :body)

An email message is returned as a Clojure map that looks something like this (with body removed)


(def m (dissoc (first (inbox 1)) :body)) ;; =>

;; =>

;; {:subject "Re: Presents for Dale's baby",
;;  :from "Someone <someone@aol.com>",
;;  :date-recieved "Tue Mar 11 12:54:41 GMT 2014",
;;  :to ("owain@owainlewis.com"),
;;  :multipart? true,
;;  :content-type "multipart/ALTERNATIVE",
;;  :sender "Someone <someone@aol.com>",
;;  :date-sent "Tue Mar 11 12:54:36 GMT 2014"}

Parser

HTML emails are evil. There is a simple HTML -> Plain text parser provided if you need to do any machine learning type processing on email messages.

(require '[clojure-mail.parser :refer :all])

(html->text "<h1>I HATE HTML EMAILS</h1>")

;; => "I HATE HTML EMAILS"

Reading emails from disk

Clojure mail can be used to parse existing email messages from file. Take a look in test/fixtures to see some example messages. To read one of these messages we can do something like this


(def message (read-mail-from-file "test/clojure_mail/fixtures/25"))

(read-message message)

;; => 
;; {:subject "Request to share ContractsBuilder", 
;; :from nil, :date-recieved nil, 
;; :to "zaphrauk@gmail.com", 
;; :multipart? true, 
;; :content-type "multipart/alternative; boundary=90e6ba1efefc44ffe804a5e76c56", 
;; :sender nil, 
;; :date-sent "Fri Jun 17 13:21:19 BST 2011" ..............
 

License

Copyright © 2014 Owain Lewis

Distributed under the Eclipse Public License, the same as Clojure.

Can you improve this documentation? These fine people already did:
owainlewis, owain lewis, Matt Gauger, James Hughes, Owain Lewis, jaimeagudo & Noah Cantor
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close