Articles Feed

Authors

Categories

MMEmail: My First Clojure Open Source Contribution

by: micah | April 21st, 2010 |

While working on website for my sister, I was perturbed that there were no libraries on Clojars for sending email. I was using Leiningen which will download and include all your dependancies for you (so sweet!). Yet, with the library I found, I had to manually download all the required jars. Call me lazy but being forced to manually download dependancies seems like cruel and unusual punishment these days.

So I solved the problem:

MMEmail

Simple Clojure library for sending email, with just one jar!

Installation

Leiningan:

  1. (defproject your-project "0.0.0"
  2. :dependencies [[mmemail "1.0.0"]]

Jar File

…can be downloaded at http://clojars.org/repo/mmemail/mmemail/1.0.0/mmemail-1.0.0.jar

Usage:

Include the library

  1. (use 'mmemail.core)

The Easy Way

mmemail.core is the only include you need. It contains only 2 methods, the fist being send-email. It takes a map that includes all the configuration and email parameters. That’s it.

  1. (send-email {:host "smtp.gmail.com"
  2. :port 465
  3. :ssl true
  4. :user "road@gmail.com"
  5. :password "runner"
  6. :to "joe@acme.com"
  7. :subject "Greetings"
  8. :body "Meep Meep!"})

Most of the parameters are required, but based on your server configuration you might get away without these:

It also accepts the following optional parameters:

The recipient parameters (:to, :cc, :bcc) may be a string, for one recipient, or a sequence of strings, for multiple recipients.

The Convenient Way

It can be annoying to pass such a big map into the function, and typically you’ll want to get all the server configuration out of the way. This is where the second function of mmemail.core (create-mailer) comes into play.

  1. (def my_mailer (create-mailer {:host "smtp.gmail.com"
  2. :port 465
  3. :ssl true
  4. :user "road@gmail.com"
  5. :password "runner"}))

create-mailer will return a new function with all the configuration baked in. So in the future, you can send email like so:

  1. (my_mailer {:to "joe@acme.com"
  2. :subject "Greetings"
  3. :body "Meep Meep!"})

The create-mailer accepts email parameters that will be used as defaults when the generated function is called.

  1. (def my_mailer (create-mailer {:subject "Greetings"
  2. :body "Meep Meep!"
  3. :host "smtp.gmail.com"
  4. :port 465
  5. :ssl true
  6. :user "road@gmail.com"
  7. :password "runner"}))
  8. (my_mailer {:to "joe@acme.com"})

License

Copyright 2010 Micah Martin. All Rights Reserved. MMEmail and all included source files are distributed under terms of the GNU LGPL.

Leave a Reply

#