GithubHelp home page GithubHelp logo

mukhtar / clojure-mail Goto Github PK

View Code? Open in Web Editor NEW

This project forked from owainlewis/clojure-mail

0.0 1.0 0.0 427 KB

A Clojure library mainly aimed at parsing, downloading and reading email from Gmail servers.

Clojure 100.00%

clojure-mail's Introduction

Clojure-mail

Clojars Project

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.

Quickstart

Add the following dependency to lein

[io.forward/clojure-mail "1.0"]

This is a complete example showing how to read the subject of your latest Gmail inbox message

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

(def store (gen-store "[email protected]" "password"))

(def inbox-messages (inbox store))

;; to convert a javamail message into a clojure message we need to call read-message

(def latest (read-message (first inbox-messages)))

;; Let's read the subject of our lastest inbox message
(:subject latest))

(keys latest)
;; => (:subject :from :date-recieved :to :multipart? :content-type :sender :date-sent :body)

Use

We need to require clojure-mail.core before we begin.

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

The first thing we need is a mail store which acts as a gateway to our gmail account. To create store we only need a gmail username and password

(def store (gen-store "[email protected]" "mypassword"))

Now we can fetch email messages from Gmail easily.

(def my-inbox-messages (take 5 (all-messages store :inbox)))

(def first-message (first my-inbox-messages))

(message/subject first-message) ;; => "Hi! Here are your new links from the weekend"

Note that the messages returned are Java mail message objects.

Reading email messages

(def javamail-message (first inbox-messages))

;; To read the entire message as a clojure map
(def message (read-message javamail-message))

;; There are also individual methods available in the message namespace. I.e to read the subject
;; of a javax.mail message

(message/subject javamail-message)

An email message returned as a Clojure map from read-message looks something like this:

{:subject "Re: Presents for Dale's baby",
 :from "Someone <[email protected]>",
 :date-recieved "Tue Mar 11 12:54:41 GMT 2014",
 :to ("[email protected]"),
 :multipart? true,
 :content-type "multipart/ALTERNATIVE",
 :sender "Someone <[email protected]>",
 :date-sent "Tue Mar 11 12:54:36 GMT 2014"
 :body [{:content-type "text/plain" :body "..."}
        {:content-type "text/html"  :body "..."}]}

Searching your inbox

You can easily search your inbox for messages

(def s (gen-store "[email protected]" "password"))
(def results (search-inbox s "projects"))

(->> results first subject) ;; => "Open Source Customisation Projects"

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 "[email protected]",
;; :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.

clojure-mail's People

Contributors

andrewmacquarrie avatar anujsrc avatar christianblunden avatar devth avatar juhani-hietikko avatar kouphax avatar mathias avatar owainlewis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.