mandrill-0.5.7.0: Library for interfacing with the Mandrill JSON API
Safe HaskellSafe-Inferred
LanguageHaskell2010

Network.API.Mandrill

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

Documentation

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.

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.

newTextMessage Source #

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.

newHtmlMessage Source #

Arguments

:: EmailAddress

Sender email

-> [EmailAddress]

Receivers email

-> Text

Subject

-> Html

The HTML body

-> MandrillMessage 

Create a new HTML message.

newTemplateMessage Source #

Arguments

:: EmailAddress

Sender email

-> [EmailAddress]

Receivers email

-> Text

Subject

-> MandrillMessage 

Create a new template message (no HTML).

newTemplateMessage' Source #

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

Expand
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 liftIO, we would have ended up with this error:

• 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 IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

> 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)