mime-mail-ses-0.4.1/Network/0000755000000000000000000000000012635441665014006 5ustar0000000000000000mime-mail-ses-0.4.1/Network/Mail/0000755000000000000000000000000012635441665014670 5ustar0000000000000000mime-mail-ses-0.4.1/Network/Mail/Mime/0000755000000000000000000000000013265570543015555 5ustar0000000000000000mime-mail-ses-0.4.1/Network/Mail/Mime/SES.hs0000644000000000000000000001620113265570543016543 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE CPP #-} module Network.Mail.Mime.SES ( sendMailSES , sendMailSESGlobal , renderSendMailSES , renderSendMailSESGlobal , SES (..) , usEast1 , usWest2 , euWest1 , SESException (..) ) where import Control.Exception (Exception, throwIO) import Control.Monad.IO.Class (MonadIO, liftIO) import Crypto.Hash (Digest, SHA256, hmac, hmacGetDigest) import Data.Byteable (toBytes) import Data.ByteString (ByteString) import Data.ByteString.Base64 (encode) import qualified Data.ByteString.Char8 as S8 import qualified Data.ByteString.Lazy as L import Data.Conduit (Sink, await, ($$), (=$)) import Data.Text (Text) import qualified Data.Text as T import Data.Time (getCurrentTime) import Data.Time.Format (formatTime) import Data.Typeable (Typeable) import Data.XML.Types (Content (ContentText), Event (EventBeginElement, EventContent)) import Network.HTTP.Client (Manager, #if MIN_VERSION_http_client(0, 5, 0) parseRequest, #else checkStatus, parseUrl, #endif requestHeaders, responseBody, responseStatus, urlEncodedBody, withResponse) import Network.HTTP.Client.Conduit (bodyReaderSource) import Network.HTTP.Types (Status) import Network.HTTP.Client.TLS (getGlobalManager) import Network.Mail.Mime (Mail, renderMail') import Text.XML.Stream.Parse (def, parseBytes) #if MIN_VERSION_time(1,5,0) import Data.Time (defaultTimeLocale) #else import System.Locale (defaultTimeLocale) #endif data SES = SES { sesFrom :: !ByteString , sesTo :: ![ByteString] , sesAccessKey :: !ByteString , sesSecretKey :: !ByteString , sesSessionToken :: !(Maybe ByteString) , sesRegion :: !Text } deriving Show renderSendMailSES :: MonadIO m => Manager -> SES -> Mail -> m () renderSendMailSES m ses mail = liftIO (renderMail' mail) >>= sendMailSES m ses -- | @since 0.4.1 -- Same as 'renderSendMailSES' but uses the global 'Manager'. renderSendMailSESGlobal :: MonadIO m => SES -> Mail -> m () renderSendMailSESGlobal ses mail = do mgr <- liftIO getGlobalManager renderSendMailSES mgr ses mail sendMailSES :: MonadIO m => Manager -> SES -> L.ByteString -- ^ Raw message data. You must ensure that -- the message format complies with -- Internet email standards regarding -- email header fields, MIME types, and -- MIME encoding. -> m () sendMailSES manager ses msg = liftIO $ do now <- getCurrentTime let date = S8.pack $ format now sig = makeSig date $ sesSecretKey ses region = T.unpack $ sesRegion ses #if MIN_VERSION_http_client(0, 5, 0) req' <- parseRequest $ concat ["https://email.", region , ".amazonaws.com"] #else req' <- parseUrl $ concat ["https://email.", region , ".amazonaws.com"] #endif let auth = S8.concat [ "AWS3-HTTPS AWSAccessKeyId=" , sesAccessKey ses , ", Algorithm=HmacSHA256, Signature=" , sig ] let req = urlEncodedBody qs $ req' { requestHeaders = [ ("Date", date) , ("X-Amzn-Authorization", auth) ] ++ case sesSessionToken ses of Just token -> [("X-Amz-Security-Token", token)] Nothing -> [] #if !MIN_VERSION_http_client(0, 5, 0) , checkStatus = \_ _ _ -> Nothing #endif } withResponse req manager $ \res -> bodyReaderSource (responseBody res) $$ parseBytes def =$ checkForError (responseStatus res) where qs = ("Action", "SendRawEmail") : ("Source", sesFrom ses) : ("RawMessage.Data", encode $ S8.concat $ L.toChunks msg) : zipWith mkDest [1 :: Int ..] (sesTo ses) mkDest num addr = (S8.pack $ "Destinations.member." ++ show num, addr) format = formatTime defaultTimeLocale "%a, %e %b %Y %H:%M:%S %z" -- | @since 0.4.1 -- Same as 'sendMailSES' but uses the global 'Manager'. sendMailSESGlobal :: MonadIO m => SES -> L.ByteString -- ^ Raw message data. You must ensure that -- the message format complies with -- Internet email standards regarding -- email header fields, MIME types, and -- MIME encoding. -> m () sendMailSESGlobal ses msg = do mgr <- liftIO getGlobalManager sendMailSES mgr ses msg checkForError :: Status -> Sink Event IO () checkForError status = do name <- getFirstStart if name == errorResponse then loop "" "" "" else return () where errorResponse = "{http://ses.amazonaws.com/doc/2010-12-01/}ErrorResponse" getFirstStart = do mx <- await case mx of Nothing -> return errorResponse Just (EventBeginElement name _) -> return name _ -> getFirstStart loop code msg reqid = await >>= maybe finish go where getContent front = do mx <- await case mx of Just (EventContent (ContentText t)) -> getContent (front . (t:)) _ -> return $ T.concat $ front [] go (EventBeginElement "{http://ses.amazonaws.com/doc/2010-12-01/}Code" _) = do t <- getContent id loop t msg reqid go (EventBeginElement "{http://ses.amazonaws.com/doc/2010-12-01/}Message" _) = do t <- getContent id loop code t reqid go (EventBeginElement "{http://ses.amazonaws.com/doc/2010-12-01/}RequestId" _) = do t <- getContent id loop code msg t go _ = loop code msg reqid finish = liftIO $ throwIO SESException { seStatus = status , seCode = code , seMessage = msg , seRequestId = reqid } -- | -- -- Exposed since: 0.3.2 data SESException = SESException { seStatus :: !Status , seCode :: !Text , seMessage :: !Text , seRequestId :: !Text } deriving (Show, Typeable) instance Exception SESException makeSig :: ByteString -> ByteString -> ByteString makeSig payload key = encode $ toBytes (hmacGetDigest $ hmac key payload :: Digest SHA256) usEast1 :: Text usEast1 = "us-east-1" usWest2 :: Text usWest2 = "us-west-2" euWest1 :: Text euWest1 = "eu-west-1" mime-mail-ses-0.4.1/LICENSE0000644000000000000000000000207512635441665013366 0ustar0000000000000000Copyright (c) 2014 Michael Snoyman, http://www.yesodweb.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mime-mail-ses-0.4.1/Setup.hs0000644000000000000000000000005612635441665014012 0ustar0000000000000000import Distribution.Simple main = defaultMain mime-mail-ses-0.4.1/mime-mail-ses.cabal0000644000000000000000000000265113265570543016002 0ustar0000000000000000Name: mime-mail-ses Version: 0.4.1 Synopsis: Send mime-mail messages via Amazon SES description: Hackage documentation generation is not reliable. For up to date documentation, please see: . Homepage: http://github.com/snoyberg/mime-mail License: MIT License-file: LICENSE Author: Michael Snoyman Maintainer: michael@snoyman.com Category: Web Build-type: Simple Cabal-version: >=1.6 extra-source-files: ChangeLog.md README.md Library Exposed-modules: Network.Mail.Mime.SES Build-depends: base >= 4.9 && < 5 , base64-bytestring >= 0.1 , bytestring >= 0.9 , time >= 1.1 , old-locale , http-client >= 0.2.2.2 , http-client-tls >= 0.2.4 , http-conduit >= 2.1 , mime-mail >= 0.3 , transformers >= 0.2 , http-types >= 0.6.8 , xml-conduit , xml-types , text , conduit , cryptohash >= 0.7.3 , byteable ghc-options: -Wall mime-mail-ses-0.4.1/ChangeLog.md0000644000000000000000000000100313265570543014516 0ustar0000000000000000## 0.4.1 * Add 'renderSendMailSESGlobal' and 'sendMailSESGlobal' function which uses the global manager. ## 0.4.0.0 * Support IAM temp credentials. This is a backwards-incompatible change that adds a field to the SES type to represent (optional) session tokens when using roles attached to EC2 instances. * Make fields in the `SES` data type strict to promote warnings to errors. ## 0.3.2.3 http-client 0.5 support ## 0.3.2.2 `time` 1.5 support ## 0.3.2 Expose `SESException` datatype and constructor. mime-mail-ses-0.4.1/README.md0000644000000000000000000000007112635441665013632 0ustar0000000000000000## mime-mail-ses Send mime-mail messages via Amazon SES