git.iko.soy/typeable/mandrill
on master help sign in
Brushed a bit the docs
commitc429a66f4ed86497406ba11ea3a844ad08b647b3 authorAlfredo Di Napoli <alfredo.dinapoli@gmail.com> dateSun 7 Sep 2014, 18:28 (12 years ago) parent53ec486
4 files changed+63−9
README.md+7−2 examples/Send.hs+1−1 mandrill.cabal+1−1 src/Network/API/Mandrill.hs+54−5
README.md+7−2
@@ -9,7 +9,7 @@ the [Mandrill](http://mandrillapp.com) transactional email service.
99# Example
1010
1111This 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
1313which can be plugged in your stack of choice.
1414Example:
1515
@@ -24,7 +24,7 @@ main = do
2424 Left err -> print $ "Invalid email!" ++ show err
2525 Right addr -> runMandrill "MYTOKENHERE" $ do
2626 let msg = "<p>My Html</p>"
27− res <- sendEmail (newTextMessage addr addr "Hello" msg)
27+ res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
2828 case res of
2929 MandrillSuccess k -> liftIO (print k)
3030 MandrillFailure f -> liftIO (print f)
@@ -60,3 +60,8 @@ And finally execute the testsuite:
6060```
6161cabal test
6262```
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
77 Left err -> print $ "Invalid email!" ++ show err
88 Right addr -> runMandrill "SUPER-SECRET-TOKEN" $ do
99 let msg = "<p>My Html</p>"
10− res <- sendEmail (newTextMessage addr addr "Hello" msg)
10+ res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
1111 case res of
1212 MandrillSuccess k -> liftIO (print k)
1313 MandrillFailure f -> liftIO (print f)
mandrill.cabal+1−1
@@ -1,5 +1,5 @@
11name: mandrill
2−version: 0.1.0.0
2+version: 0.1.0.1
33synopsis: Library for interfacing with the Mandrill JSON API
44description: Pure Haskell client for the Mandrill JSON API
55license: 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+-}
18
29module Network.API.Mandrill (
310 module M
@@ -6,6 +13,9 @@ module Network.API.Mandrill (
613 , newTextMessage
714 , newHtmlMessage
815 , liftIO
16+
17+ -- * Appendix: Example Usage
18+ -- $exampleusage
919 ) where
1020
1121import Control.Monad.Reader
@@ -21,16 +31,39 @@ import Text.Email.Validate
2131import qualified Data.Text as T
2232import qualified Data.Aeson as JSON
2333
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+-}
2454
2555--------------------------------------------------------------------------------
26−emptyMessage :: EmailAddress -> EmailAddress -> MandrillMessage
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.
59+emptyMessage :: EmailAddress -> [EmailAddress] -> MandrillMessage
2760emptyMessage f t = MandrillMessage {
2861 _mmsg_html = mempty
2962 , _mmsg_text = Nothing
3063 , _mmsg_subject = T.empty
3164 , _mmsg_from_email = f
3265 , _mmsg_from_name = Nothing
33− , _mmsg_to = [newRecipient t]
66+ , _mmsg_to = map newRecipient t
3467 , _mmsg_headers = JSON.Null
3568 , _mmsg_important = Nothing
3669 , _mmsg_track_opens = Nothing
@@ -59,22 +92,33 @@ emptyMessage f t = MandrillMessage {
5992 }
6093
6194
62−
6395--------------------------------------------------------------------------------
96+-- | Create a new HTML message.
6497newHtmlMessage :: EmailAddress
65− -> EmailAddress
98+ -- ^ Sender email
99+ -> [EmailAddress]
100+ -- ^ Receivers email
66101 -> T.Text
102+ -- ^ Subject
67103 -> Html
104+ -- ^ The HTML body
68105 -> MandrillMessage
69106newHtmlMessage f t subj html = let body = mkMandrillHtml html in
70107 ((mmsg_html .~ body) . (mmsg_subject .~ subj)) $ (emptyMessage f t)
71108
72109
73110--------------------------------------------------------------------------------
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.
74114newTextMessage :: EmailAddress
75− -> EmailAddress
115+ -- ^ Sender email
116+ -> [EmailAddress]
117+ -- ^ Receivers email
76118 -> T.Text
119+ -- ^ Subject
77120 -> T.Text
121+ -- ^ The body, as normal text.
78122 -> MandrillMessage
79123newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
80124 ((mmsg_html .~ body) .
@@ -83,6 +127,11 @@ newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
83127
84128
85129--------------------------------------------------------------------------------
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.
86135sendEmail :: MonadIO m
87136 => MandrillMessage
88137 -> MandrillT m (MandrillResponse [MessagesResponse])