@@ -1,3 +1,10 @@
1+{-| This package is an attempt to expose the Mandrill JSON API in pure Haskell.
2+ To do that, the library API comes in two flavours:
4+ * An IO-based, low-level 1:1 mapping of the JSON API,
5+ as described on <https://mandrillapp.com/api/docs/ the website>.
6+ * A handy monad transformer which can be plugged in your stack of choice.
2module Network.API.Mandrill (
9module Network.API.Mandrill (
@@ -6,6 +13,9 @@ module Network.API.Mandrill (
6 , newTextMessage
13 , newTextMessage
7 , newHtmlMessage
14 , newHtmlMessage
17+ -- * Appendix: Example Usage
11import Control.Monad.Reader
21import Control.Monad.Reader
@@ -21,16 +31,39 @@ import Text.Email.Validate
21import qualified Data.Text as T
31import qualified Data.Text as T
22import qualified Data.Aeson as JSON
32import qualified Data.Aeson as JSON
36+The API was designed to allow to get you started as quickly as possible:
38+> {-# LANGUAGE OverloadedStrings #-}
39+> import Text.Email.Validate
40+> import Network.API.Mandrill
44+> case validate "foo@example.com" of
45+> Left err -> print $ "Invalid email!" ++ show err
46+> Right addr -> runMandrill "MYTOKENHERE" $ do
47+> let msg = "<p>My Html</p>"
48+> res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
50+> MandrillSuccess k -> liftIO (print k)
51+> MandrillFailure f -> liftIO (print f)
25--------------------------------------------------------------------------------
55--------------------------------------------------------------------------------
56+-- | Builds an empty message, given only the email of the sender and
57+-- the emails of the receiver. Please note that the "Subject" will be empty,
58+-- so you need to use either @newTextMessage@ or @newHtmlMessage@ to populate it.
26−emptyMessage :: EmailAddress -> EmailAddress -> MandrillMessage
59+emptyMessage :: EmailAddress -> [EmailAddress] -> MandrillMessage
27emptyMessage f t = MandrillMessage {
60emptyMessage f t = MandrillMessage {
28 _mmsg_html = mempty
61 _mmsg_html = mempty
29 , _mmsg_text = Nothing
62 , _mmsg_text = Nothing
30 , _mmsg_subject = T.empty
63 , _mmsg_subject = T.empty
31 , _mmsg_from_email = f
64 , _mmsg_from_email = f
32 , _mmsg_from_name = Nothing
65 , _mmsg_from_name = Nothing
33− , _mmsg_to = [newRecipient t]
66+ , _mmsg_to = map newRecipient t
34 , _mmsg_headers = JSON.Null
67 , _mmsg_headers = JSON.Null
35 , _mmsg_important = Nothing
68 , _mmsg_important = Nothing
36 , _mmsg_track_opens = Nothing
69 , _mmsg_track_opens = Nothing
@@ -59,22 +92,33 @@ emptyMessage f t = MandrillMessage {
63--------------------------------------------------------------------------------
95--------------------------------------------------------------------------------
96+-- | Create a new HTML message.
64newHtmlMessage :: EmailAddress
97newHtmlMessage :: EmailAddress
65− -> EmailAddress
99+ -> [EmailAddress]
100+ -- ^ Receivers email
66 -> T.Text
101 -> T.Text
68 -> MandrillMessage
105 -> MandrillMessage
69newHtmlMessage f t subj html = let body = mkMandrillHtml html in
106newHtmlMessage f t subj html = let body = mkMandrillHtml html in
70 ((mmsg_html .~ body) . (mmsg_subject .~ subj)) $ (emptyMessage f t)
107 ((mmsg_html .~ body) . (mmsg_subject .~ subj)) $ (emptyMessage f t)
73--------------------------------------------------------------------------------
110--------------------------------------------------------------------------------
111+-- | Create a new textual message. By default Mandrill doesn't require you
112+-- to specify the @mmsg_text@ when sending out the JSON Payload, and this
113+-- function ensure it will be present.
74newTextMessage :: EmailAddress
114newTextMessage :: EmailAddress
75− -> EmailAddress
116+ -> [EmailAddress]
117+ -- ^ Receivers email
76 -> T.Text
118 -> T.Text
77 -> T.Text
120 -> T.Text
121+ -- ^ The body, as normal text.
78 -> MandrillMessage
122 -> MandrillMessage
79newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
123newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
80 ((mmsg_html .~ body) .
124 ((mmsg_html .~ body) .
@@ -83,6 +127,11 @@ newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
85--------------------------------------------------------------------------------
129--------------------------------------------------------------------------------
130+-- | The simplest way to use the API. All you need to provide is a valid
131+-- 'MandrillMessage' and this function will send an email inside a
132+-- 'MandrillT' transformer. You are not forced to use the 'MandrillT' context
133+-- though. Have a look at "Network.API.Mandrill.Messages" for an IO-based,
134+-- low lever function for sending email.
86sendEmail :: MonadIO m
135sendEmail :: MonadIO m
87 => MandrillMessage
136 => MandrillMessage
88 -> MandrillT m (MandrillResponse [MessagesResponse])
137 -> MandrillT m (MandrillResponse [MessagesResponse])