A Clojure wrapper for mailgun API.
[nilenso/mailgun "0.2.3"]
<dependency>
<groupId>nilenso</groupId>
<artifactId>mailgun</artifactId>
<version>0.2.2</version>
</dependency>
(: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 "bar.com"})
(def content {:from "[email protected]"
:to "[email protected]"
: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.
(mail/send-mail creds content)
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)
:body)
recipients (mail/parse ["To" "Cc" "Bcc"] msg-body)
message (mail/parse-message msg-body)]
(println recipients)
(println message))
=> {:to "[email protected]" :bcc "[email protected]" :cc nil}
=> {:sender "[email protected]", :to "[email protected]", :bcc "[email protected]", :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" "https://api.mailgun.net/v2/domains/foomail.in/messages/eyJRhImsiOiAiZ1IiwgInMiOiAiNmNlTQ3NTY4ZGZSwg0MWRmLWEwODQtNzCJjIjogImJpZ3RhbmtzMiJMtMzQ0OC0NWJiY2Q4ODQMDkwMzk4ZCIsIwIjogdHJ19/attachments/0", "content-type" "image/jpeg", "name" "Image1.jpg", "size" 267928} {"url" "https://api.mailgun.net/v2/domains/foomail.in/messages/eyJRhImsiOiAiZ1IiwgInMiOiAiNmNlTQ3NTY4ZGZSwg0MWRmLWEwODQtNzCJjIjogImJpZ3RhbmtzMiJMtMzQ0OC0NWJiY2Q4ODQMDkwMzk4ZCIsIwIjogdHJ19/attachments/1", "content-type" "image/jpeg", "name" "Image2.jpg", "size" 477946}]}
The download-attachment
function can be used to download attachments give the attachment url and the credentials
(let [attch-url "https://api.mailgun.net/v2/domains/foomail.in/messages/eyJRhImsiOiAiZ1IiwgInMiOiAiNmNlTQ3NTY4ZGZSwg0MWRmLWEwODQtNzCJjIjogImJpZ3RhbmtzMiJMtMzQ0OC0NWJiY2Q4ODQMDkwMzk4ZCIsIwIjogdHJ19/attachments/0"]
(mail/download-attachment creds attch-url))
=> #object[java.io.BufferedInputStream 0xffb1f95 "java.io.BufferedInputStream@ffb1f95"]
Copyright © 2016 Nilenso
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.