Mailgun API in clojure
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Clojars Project

A Clojure wrapper for mailgun API.


[nilenso/mailgun "0.2.3"]




(:require [mailgun.mail :as mail]
          [mailgun.util :refer [to-file]])

The send-mail function takes two argument mailgun credentials and email content which has to be given in the following format

(def creds {:key "key-3ax6xnjp29jd6fds4gc373sgvjxteol1" :domain ""})

(def content {:from ""
              :to ""
              :subject "Test"
              :html "test body"
              :attachment (to-file ["/path/to/file1.doc" "/path/to/file2.doc"])})

The value of the :attachment has to be a vector of files to be attached. If there are no files to be attached then don't include the :attachment keyword in the content.

send message

(mail/send-mail creds content)

get messages

There are functions that help you retrieve stored messages from mailgun and parse them as required and also download attachments if any.

  • get-stored-message

The get-stored-message function gets the complete mail response from mailgun.

(mail/get-stored-message creds msg-key)
  • parse

This helps parse the :body of the mail

(mail/parse ["subject"] msg-body)
  • parse-message

This is wrapper over the parse function, which parses the basic fields like - to, sender, bcc, cc, subject, date, body-html and attachments.

(mail/parse-message msg-body)

Here is an example -

(let [msg-key "eyJRhImsiOiAiZ1IiwgInMiOiAiNmNlTQ3NTY4ZGZSwg0MWRmLWEwODQtNzCJjIjogImJpZ3RhbmtzMiJMtMzQ0OC0NWJiY2Q4ODQMDkwMzk4ZCIsIwIjogdHJ19"
      msg-body (->> msg-key
                    (mail/get-stored-message creds)
      recipients (mail/parse ["To" "Cc" "Bcc"] msg-body)
      message (mail/parse-message msg-body)]
  (println recipients)
  (println message))
=> {:to "" :bcc "" :cc nil}
=> {:sender "", :to "", :bcc "", :cc nil, :subject "Message Subject", :date "Mon, 2 May 2016 14:43:28 +0530", :body-html "<div dir=\"ltr\"><br clear=\"all\"><div><br></div><br>\r\n</div>\r\n", :attachments [{"url" "", "content-type" "image/jpeg", "name" "Image1.jpg", "size" 267928} {"url" "", "content-type" "image/jpeg", "name" "Image2.jpg", "size" 477946}]}

download attachments

The download-attachment function can be used to download attachments give the attachment url and the credentials

(let [attch-url ""]
  (mail/download-attachment creds attch-url))
=> #object[ 0xffb1f95 ""]


Copyright © 2016 Nilenso

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.