git.iko.soy/typeable/mandrill
on master help sign in
Brushed a bit the docs
c429a66Alfredo Di Napoli
README.md+7−2
@@ -9,7 +9,7 @@ the [Mandrill](http://mandrillapp.com) transactional email service.
9# Example
9# Example
10
10
11This package was built with pragmatism and reuse in mind. This means
11This package was built with pragmatism and reuse in mind. This means
12−this API comes in two flavours: an IO-based and an handy monad transformers
12+this API comes in two flavours: an IO-based and an handy monad transformer
13which can be plugged in your stack of choice.
13which can be plugged in your stack of choice.
14Example:
14Example:
15
15
@@ -24,7 +24,7 @@ main = do
24 Left err -> print $ "Invalid email!" ++ show err
24 Left err -> print $ "Invalid email!" ++ show err
25 Right addr -> runMandrill "MYTOKENHERE" $ do
25 Right addr -> runMandrill "MYTOKENHERE" $ do
26 let msg = "<p>My Html</p>"
26 let msg = "<p>My Html</p>"
27− res <- sendEmail (newTextMessage addr addr "Hello" msg)
27+ res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
28 case res of
28 case res of
29 MandrillSuccess k -> liftIO (print k)
29 MandrillSuccess k -> liftIO (print k)
30 MandrillFailure f -> liftIO (print f)
30 MandrillFailure f -> liftIO (print f)
@@ -60,3 +60,8 @@ And finally execute the testsuite:
60```
60```
61cabal test
61cabal test
62```
62```
63+
64+# Contributions
65+This library scratches my own itches, but please fork away!
66+Pull requests are encouraged to implement the part of the API
67+you need.
examples/Send.hs+1−1
@@ -7,7 +7,7 @@ main = case validate "foo@example.com" of
7 Left err -> print $ "Invalid email!" ++ show err
7 Left err -> print $ "Invalid email!" ++ show err
8 Right addr -> runMandrill "SUPER-SECRET-TOKEN" $ do
8 Right addr -> runMandrill "SUPER-SECRET-TOKEN" $ do
9 let msg = "<p>My Html</p>"
9 let msg = "<p>My Html</p>"
10− res <- sendEmail (newTextMessage addr addr "Hello" msg)
10+ res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
11 case res of
11 case res of
12 MandrillSuccess k -> liftIO (print k)
12 MandrillSuccess k -> liftIO (print k)
13 MandrillFailure f -> liftIO (print f)
13 MandrillFailure f -> liftIO (print f)
mandrill.cabal+1−1
@@ -1,5 +1,5 @@
1name: mandrill
1name: mandrill
2−version: 0.1.0.0
2+version: 0.1.0.1
3synopsis: Library for interfacing with the Mandrill JSON API
3synopsis: Library for interfacing with the Mandrill JSON API
4description: Pure Haskell client for the Mandrill JSON API
4description: Pure Haskell client for the Mandrill JSON API
5license: MIT
5license: MIT
src/Network/API/Mandrill.hs+54−5
@@ -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:
3+
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.
7+-}
1
8
2module Network.API.Mandrill (
9module Network.API.Mandrill (
3 module M
10 module M
@@ -6,6 +13,9 @@ module Network.API.Mandrill (
6 , newTextMessage
13 , newTextMessage
7 , newHtmlMessage
14 , newHtmlMessage
8 , liftIO
15 , liftIO
16+
17+ -- * Appendix: Example Usage
18+ -- $exampleusage
9 ) where
19 ) where
10
20
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
23
33
34+{- $exampleusage
35+
36+The API was designed to allow to get you started as quickly as possible:
37+
38+> {-# LANGUAGE OverloadedStrings #-}
39+> import Text.Email.Validate
40+> import Network.API.Mandrill
41+>
42+> main :: IO ()
43+> main = do
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)
49+> case res of
50+> MandrillSuccess k -> liftIO (print k)
51+> MandrillFailure f -> liftIO (print f)
52+
53+-}
24
54
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 {
59 }
92 }
60
93
61
94
62−
63--------------------------------------------------------------------------------
95--------------------------------------------------------------------------------
96+-- | Create a new HTML message.
64newHtmlMessage :: EmailAddress
97newHtmlMessage :: EmailAddress
98+ -- ^ Sender email
65− -> EmailAddress
99+ -> [EmailAddress]
100+ -- ^ Receivers email
66 -> T.Text
101 -> T.Text
102+ -- ^ Subject
67 -> Html
103 -> Html
104+ -- ^ The HTML body
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)
71
108
72
109
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
115+ -- ^ Sender email
75− -> EmailAddress
116+ -> [EmailAddress]
117+ -- ^ Receivers email
76 -> T.Text
118 -> T.Text
119+ -- ^ Subject
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
83
127
84
128
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])