| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Network.API.Mandrill
Contents
Description
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 the website.
- A handy monad transformer which can be plugged in your stack of choice.
Synopsis
- module Network.API.Mandrill.Types
- module Network.API.Mandrill.Trans
- module Network.API.Mandrill.Messages.Types
- module Network.API.Mandrill.Messages
- sendEmail :: MonadIO m => MandrillMessage -> MandrillT m (MandrillResponse [MessagesResponse])
- sendTextEmail :: MonadIO m => MandrillMessage -> MandrillT m (MandrillResponse [MessagesResponse])
- emptyMessage :: Maybe EmailAddress -> [EmailAddress] -> MandrillMessage
- newTextMessage :: EmailAddress -> [EmailAddress] -> Text -> Text -> MandrillMessage
- newHtmlMessage :: EmailAddress -> [EmailAddress] -> Text -> Html -> MandrillMessage
- newTemplateMessage :: EmailAddress -> [EmailAddress] -> Text -> MandrillMessage
- newTemplateMessage' :: [EmailAddress] -> MandrillMessage
- liftIO :: MonadIO m => IO a -> m a
Documentation
module Network.API.Mandrill.Types
module Network.API.Mandrill.Trans
sendEmail :: MonadIO m => MandrillMessage -> MandrillT m (MandrillResponse [MessagesResponse]) Source #
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 level function for sending email.
sendTextEmail :: MonadIO m => MandrillMessage -> MandrillT m (MandrillResponse [MessagesResponse]) Source #
emptyMessage :: Maybe EmailAddress -> [EmailAddress] -> MandrillMessage Source #
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.
Arguments
| :: EmailAddress | Sender email |
| -> [EmailAddress] | Receivers email |
| -> Text | Subject |
| -> Text | The body, as normal text. |
| -> MandrillMessage |
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.
Arguments
| :: EmailAddress | Sender email |
| -> [EmailAddress] | Receivers email |
| -> Text | Subject |
| -> Html | The HTML body |
| -> MandrillMessage |
Create a new HTML message.
Arguments
| :: EmailAddress | Sender email |
| -> [EmailAddress] | Receivers email |
| -> Text | Subject |
| -> MandrillMessage |
Create a new template message (no HTML).
Arguments
| :: [EmailAddress] | Receivers email |
| -> MandrillMessage |
Create a new template message (no HTML) with recipient addresses only. This function is preferred when the template being used has the sender address and subject already configured in the Mandrill server.
liftIO :: MonadIO m => IO a -> m a #
Lift a computation from the IO monad.
This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations
(i.e. IO is the base monad for the stack).
Example
import Control.Monad.Trans.State -- from the "transformers" library printState :: Show s => StateT s IO () printState = do state <- get liftIO $ print state
Had we omitted , we would have ended up with this error:liftIO
• Couldn't match type ‘IO’ with ‘StateT s IO’ Expected type: StateT s IO () Actual type: IO ()
The important part here is the mismatch between StateT s IO () and .IO ()
Luckily, we know of a function that takes an and returns an IO a(m a): ,
enabling us to run the program and see the expected results:liftIO
> evalStateT printState "hello" "hello" > evalStateT printState 3 3
Appendix: Example Usage
The API was designed to allow to get you started as quickly as possible:
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)