mandrill 0.1.0.0 → 0.1.0.1
raw patch · 2 files changed
+55/−6 lines, 2 files
Files
- mandrill.cabal +1/−1
- src/Network/API/Mandrill.hs +54/−5
mandrill.cabal view
@@ -1,5 +1,5 @@ name: mandrill-version: 0.1.0.0+version: 0.1.0.1 synopsis: Library for interfacing with the Mandrill JSON API description: Pure Haskell client for the Mandrill JSON API license: MIT
src/Network/API/Mandrill.hs view
@@ -1,4 +1,11 @@+{-| This package is an attempt to expose the Mandrill JSON API in pure Haskell.+ To do that, the library API comes in two flavours: + * An IO-based, low-level 1:1 mapping of the JSON API,+ as described on <https://mandrillapp.com/api/docs/ the website>.+ * A handy monad transformer which can be plugged in your stack of choice.+-}+ module Network.API.Mandrill ( module M , sendEmail@@ -6,6 +13,9 @@ , newTextMessage , newHtmlMessage , liftIO++ -- * Appendix: Example Usage+ -- $exampleusage ) where import Control.Monad.Reader@@ -21,16 +31,39 @@ import qualified Data.Text as T import qualified Data.Aeson as JSON +{- $exampleusage +The API was designed to allow to get you started as quickly as possible:++> {-# LANGUAGE OverloadedStrings #-}+> import Text.Email.Validate+> import Network.API.Mandrill+> +> main :: IO ()+> main = do+> case validate "foo@example.com" of+> Left err -> print $ "Invalid email!" ++ show err+> Right addr -> runMandrill "MYTOKENHERE" $ do+> let msg = "<p>My Html</p>"+> res <- sendEmail (newTextMessage addr [addr] "Hello" msg)+> case res of+> MandrillSuccess k -> liftIO (print k)+> MandrillFailure f -> liftIO (print f)++-}+ ---------------------------------------------------------------------------------emptyMessage :: EmailAddress -> EmailAddress -> MandrillMessage+-- | Builds an empty message, given only the email of the sender and+-- the emails of the receiver. Please note that the "Subject" will be empty,+-- so you need to use either @newTextMessage@ or @newHtmlMessage@ to populate it.+emptyMessage :: EmailAddress -> [EmailAddress] -> MandrillMessage emptyMessage f t = MandrillMessage { _mmsg_html = mempty , _mmsg_text = Nothing , _mmsg_subject = T.empty , _mmsg_from_email = f , _mmsg_from_name = Nothing- , _mmsg_to = [newRecipient t]+ , _mmsg_to = map newRecipient t , _mmsg_headers = JSON.Null , _mmsg_important = Nothing , _mmsg_track_opens = Nothing@@ -59,22 +92,33 @@ } - --------------------------------------------------------------------------------+-- | Create a new HTML message. newHtmlMessage :: EmailAddress- -> EmailAddress+ -- ^ Sender email+ -> [EmailAddress]+ -- ^ Receivers email -> T.Text+ -- ^ Subject -> Html+ -- ^ The HTML body -> MandrillMessage newHtmlMessage f t subj html = let body = mkMandrillHtml html in ((mmsg_html .~ body) . (mmsg_subject .~ subj)) $ (emptyMessage f t) --------------------------------------------------------------------------------+-- | Create a new textual message. By default Mandrill doesn't require you+-- to specify the @mmsg_text@ when sending out the JSON Payload, and this+-- function ensure it will be present. newTextMessage :: EmailAddress- -> EmailAddress+ -- ^ Sender email+ -> [EmailAddress]+ -- ^ Receivers email -> T.Text+ -- ^ Subject -> T.Text+ -- ^ The body, as normal text. -> MandrillMessage newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in ((mmsg_html .~ body) .@@ -83,6 +127,11 @@ --------------------------------------------------------------------------------+-- | The simplest way to use the API. All you need to provide is a valid+-- 'MandrillMessage' and this function will send an email inside a+-- 'MandrillT' transformer. You are not forced to use the 'MandrillT' context+-- though. Have a look at "Network.API.Mandrill.Messages" for an IO-based,+-- low lever function for sending email. sendEmail :: MonadIO m => MandrillMessage -> MandrillT m (MandrillResponse [MessagesResponse])