![]() |
Articles Feed |
Categories
Archives
- August 2010 (1)
- July 2010 (5)
- June 2010 (4)
- April 2010 (3)
- March 2010 (2)
- February 2010 (2)
- January 2010 (1)
- December 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (1)
- July 2009 (5)
- June 2009 (2)
- May 2009 (2)
- April 2009 (8)
- March 2009 (7)
- January 2009 (2)
- December 2008 (3)
- November 2008 (5)
- October 2008 (4)
- September 2008 (6)
- August 2008 (4)
- July 2008 (5)
- June 2008 (5)
- May 2008 (4)
- April 2008 (2)
- February 2008 (4)
- January 2008 (2)
- December 2007 (2)
- November 2007 (2)
- October 2007 (2)
- September 2007 (1)
- August 2007 (3)
- July 2007 (1)
- June 2007 (4)
- May 2007 (7)
- April 2007 (2)
- February 2007 (3)
- January 2007 (3)
- November 2006 (3)
- October 2006 (3)
- September 2006 (17)
- November 2004 (1)
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:
- (defproject your-project "0.0.0"
- :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
- (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.
- (send-email {:host "smtp.gmail.com"
- :port 465
- :ssl true
- :user "road@gmail.com"
- :password "runner"
- :to "joe@acme.com"
- :subject "Greetings"
- :body "Meep Meep!"})
Most of the parameters are required, but based on your server configuration you might get away without these:
- :ssl
- :password
- :subject
It also accepts the following optional parameters:
- :from (defaults to :user)
- :cc
- :bcc
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.
- (def my_mailer (create-mailer {:host "smtp.gmail.com"
- :port 465
- :ssl true
- :user "road@gmail.com"
- :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:
- (my_mailer {:to "joe@acme.com"
- :subject "Greetings"
- :body "Meep Meep!"})
The create-mailer accepts email parameters that will be used as defaults when the generated function is called.
- (def my_mailer (create-mailer {:subject "Greetings"
- :body "Meep Meep!"
- :host "smtp.gmail.com"
- :port 465
- :ssl true
- :user "road@gmail.com"
- :password "runner"}))
- (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.
