Project: SendGrid for Clojure

TLDR: If you need to send emails via SendGrid from a Clojure application, check out the sendgrid library I just released, it’s pretty swell.

This is another simple library that just aims to do one thing well, but if you’re sending transactional emails from your webapp, you should be using something like this. There are a couple other Clojure libraries out there, but I checked them out and ran into a number of issues and inconveniences. (On that topic, I’m making it a habit to include a Motivations section in my projects’ READMEs as I know one of my first questions when evaluating a library is to ask “OK, but how is this one different from the alternatives?”)

Sending emails is simply a matter of passing API credentials and a map representing the email to send:

(require '[sendgrid.core :as sendgrid])

(def sg-config
  {:api-user "camdez"
   :api-key  "d34db33f"})

; Send a plain-text email
(sendgrid/send-email sg-config
                     {:to      "email@example.com"
                      :from    "email2@example.com"
                      :subject "SendGrid Test"
                      :text    "Email body here."})
;; => {:message "success"}

There’s nothing novel here but it works smoothly.

Actually the only particularly interesting piece to implement was attachments (simply pass a sequence under the :attachments key), for which I had to learn a bit more about multipart encoding (incidentally this is also the reason I didn’t use my preferred Aleph for the HTTP client—it doesn’t yet have multipart support). The payoff to that research is that the library can handle attachments in the full variety of formats offered by the underlying library: File, InputStream, ByteArray, or String. This means you can easy attach a file on disk or generate one dynamically.

Lastly I’ll point out that there are lots of other calls in the SendGrid API, but I didn’t implement them since I didn’t need them. But adding new calls is trivial, so please feel free to either submit a PR or open an issue on the GitHub page if you need support for something else.

I hope this is helpful to someone. Please contact me with any thoughts, questions, or concerns.